How count the number of review per post
-
hi, how can I show you a number of reviews on each of its posts?
Thanks
-
So I should and will just build this into a shortcode in the next release, however, currently you will have to modify some PHP to get this output. However, if you are doing this in concurrence with displaying reviews, you can simply count the
$reviews
array that is queried inrich-reviews.php
on line 330, and passed to/shortcodes/rr-show.php
in thehandle_show
function that starts on line 7. Simplycount($reviews)
will return the number of reviews grabbed from the database. If you want to do this separate from the[RICH_REVIEWWS_SHOW]
shortcode, then you will have to modify the plugin a bit more to add yourself a custom shortcode. However if you are willing to do that, you can have a shortcode handle that you can place anywhere.To do so, you will want to alter
rich-reviews.php
as follows.Inside the
__construct
function, at the bottom after all otheradd_action
andadd_shortcode
calls you will want to register a new shortcode. Using something like thisadd_shortcode('RICH_REVIEWS_COUNT', array(&$this, 'shortcode_reviews_count'));
Then somewhere amongst the other functions you will need to add the function
shortcode_reviews_count
that will handle the shortcode output. Something like this will do the trick.function shortcode_reviews_count() { global $post; $reviews = $this->db->get_reviews('all', 'all', $post->ID); ob_start(); ?> <span class="rr-reviews-count"> <?php echo count($reviews); ?> </span> <?php return ob_get_clean(); }
Examples of the modified code blocks are shown below.
Add action:
Add shortcode function:
Let me know if this works for you,
Cheers,
Charlie Maxwell
[NM_Developer]hi, thanks for the reply,
the above code works, but the number shown is the sum of all the review instead of each post,
I changed a little bit above code and managed ie like this :
function shortcode_reviews_count($atts) { global $post; extract(shortcode_atts( array( 'category' => 'none', 'num' => '3', 'id' => '' ) , $atts)); if ($id != '') { $id = intval($id); $passedPost = get_post($id); } if (!isset($passedPost)) { $passedPost = $post; } $reviews = $this->db->get_reviews($category, $num, $passedPost); if(!empty($reviews)) { ob_start(); ?> <span class="rr-reviews-count"> <?php echo count($reviews); ?> </span> <?php return ob_get_clean(); } else { return; } }
similar in function RICH_REVIEWS_SHOW :),
and then I can add a filter in the shortcode
[RICH_REVIEWS_COUNT category="post" id="POST ID"]
but I do not know what is the best way, maybe you can fix it and make updates to the plugin later ??
Thankyou.
Ahh, you are entirely correct the category parameter must be set to
post
orpage
. That was simply an error on my part. So with the correction, you could simply change the second function to the following:function shortcode_reviews_count() { global $post; $reviews = $this->db->get_reviews('post', 'all', $post->ID); ob_start(); ?> <span class="rr-reviews-count"> <?php echo count($reviews); ?> </span> <?php return ob_get_clean(); }
The way that you have done it works just fine as well, allowing the shortcode to be used more flexibly in a similar way that the show shortcode is set up. The only advantage here, is that you wouldn’t have to manually include the Post ID in the shortcode itself as it is programmatically grabbed from the post loaded into data context.
Again, both ways work just fine, it really depends how you intend to use them.
A shortcode like this will be included in the next update.
Cheers,
Charlie Maxwell
[NM_Developer]nice, thankyou
- The topic ‘How count the number of review per post’ is closed to new replies.