2

I want to use the shortcode below to add a review box to posts in my wordpress template:

<?php echo do_shortcode('[rwp-review id="X"]'); ?>

Where X is the Review ID of the review box for each post.

To get the Review ID, I have the code below but it is not working. $postid gets the current post ID. $box result returns array values for the review box one of which is the Review ID. If I echo $reviewid, I get the Review ID which can be 0, 1, 2, 3 and upwards. I then tried to use $reviewid in the final shortcode but It is not working. I have very little PHP knowledge so I think I inserted the code the wrong way.

<?php
        $postid = get_the_ID();
        $box = RWP_API::get_post_reviews_boxes( $postid, false );
        $reviewid = $box[0]['review_id'];

    ?>

    <?php echo do_shortcode('[rwp-review id=". $reviewid . "]'); ?>

Can anyone suggest the best approach to this?

3
  • Where are you putting your code that starts with $postid = get_the_ID()? You may need to add global $post; depending on where it is, and if it's hooked up to an action or not. Commented Apr 10, 2017 at 19:15
  • This code is inside the content-single.php file which is part of the single.php. There is global $post somewhere at the top of the file too. When I echo $postid, it gave me the correct post id value. Same thing when I echo $reviewid, I got the correct review ID too. I think the challenge is getting the value I got for review ID to be recognized as X in the shortcode Commented Apr 10, 2017 at 19:23
  • Seems like an issue with the plugin, found something similar here: wordpress.stackexchange.com/questions/200402/… - not sure what the API looks like, it's a paid plugin Commented Apr 10, 2017 at 19:35

1 Answer 1

3

Try:

<?php echo do_shortcode('[rwp-review id="'. $reviewid . '"]'); ?>

or

<?php echo do_shortcode("[rwp-review id='{$reviewid}']"); ?>

Sign up to request clarification or add additional context in comments.

2 Comments

Both of these code worked. I opted for the first one. Thanks.
Good on ya Leekie. The $reviewid var wasn't being parsed, the shortcode was literally looking for . $reviewid .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.