2

I will try to keep this short and simple as possible.

My website uses listings (instead of posts) because I have a directory page theme (MyListing - this one). I review restaurants and add reviews through "Let's Review" plugin (API documentation is here). When I review a listing the plugin adds a review box in the listing, showing pros and cons and the final score. It looks something like this:

enter image description here

What I would like is to get that final score (7.8 in this case) in raw form, without design and put it in the listing template on the top above the content (I will put it into the listing template because every listing will have a review and this box). I don't need help where in the listing template to add it, I will do that myself once I find out how to call this API code.

The 'Let's Review' theme does have API support and I did find the part of code which (I think) should suit me:

if ( class_exists( 'Lets_Review_API' ) ) {
    $lets_review_api = new Lets_Review_API();
    $lets_review_final_score = $lets_review_api->lets_review_get_final_score( $postid );
}

If I pressume correctly, this snippet calls the post ID and shows the final score in raw form. That's what I want! But here is the problem. As I understood the part "Ensure you are calling it from inside the WordPress post loop" it means I have to add this code to functions.php of the theme. But everything else is beyond me. I don't know how to call this final score in the raw form in my listing template? The documentation also states "You must pass a valid post id to the method to get the correct data" but I don't know how.

Did I do the right thing to add this code to functions.php? And which code to add to my listing template (listing.php) so it will, as I pressume, fetch the post ID and show the raw score of that review.

Any help (or taking me into the right direction) is appreciated.

1 Answer 1

1

Damn it, I even figured it out and it works.

So, I added this directly into my listing template, nothing was added into functions.php nor was any other file altered. I first called the $post variable - in the Wordpress documentation it says it "contains data from the current post in The Loop." I figured this will work as the code has to be in the loop and added it to the top.

As said in the documentation "You must pass a valid post id to the method to get the correct data" I thought to myself well, I somehow need to get that post ID before everything is executed. Therefore the $id string, which gets the Wordpress post ID.

The second part, from if ( class... to the last part $id ); is from the plugin API documentation, and it calls only the final score of the review, without any design.

But heck, this didn't show the review yet. I heard once "echo" does miracles, as it calls out the function and it shows it. So I said - why not? By Wordpress documentation echo outputs one or more strings.

And voilá - it works. I learned a lot of new things by combining different sources and it took me 3 days to figure everything out. Now it's great I know how to output this without messing with a lot of files and - it's simple.

Hope it helps someone like it did to me :)

<?php
$post = $wp_query->post;
$id = get_the_ID();
if ( class_exists( 'Lets_Review_API' ) ) {
$lets_review_api = new Lets_Review_API();
$lets_review_final_score = $lets_review_api->lets_review_get_final_score( $id );
echo $lets_review_final_score;
}
?>
3
  • 1
    Your 'if' statement checks for the class. But what if the class doesn't exist for some reason? Then the echo statement at the end will cause an error, because the variable hasn't been defined. So, put the echo commend within the 'if' statement. That will show the value, but if the value doesn't exist (if the class doesn't exist for some reason), then no value will be displayed, but you won't get an error. Probably, the class will always exist, but good programming practice will take into account what I call "Stupid Answer Detection" - getting an answer you don't expect. Commented Apr 29, 2018 at 0:15
  • Hi @RickHellewell! Thanks for your help. I have put the echo command within the if statement (although the class exists here, but like you said - you never know) so if there is no class it won't show but it also won't produce an error. I have updated the code and I hope it's correct.
    – Haris
    Commented Apr 29, 2018 at 15:39
  • looks good to me. I always like to code for 'SAD - Stupid Answer Detection'. Every decision has an answer - and it may not be one you expect, so you need to code for 'wrong' and unexpected answers. Commented Apr 29, 2018 at 21:39

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.