Show plain stars block inside the WP loop
This content is intended for WordPress developers, and it may require coding knowledge of WordPress, PHP, and JavaScript. Code examples provided here may contain errors or needs some additional coding. Make sure to test the code before using it on a live website!
If you need to display the rating block inside the WordPress Loop, you will need to get the rating snippet PHP code to place into the loop (inside the theme template).
To do that, it is highly recommended to use Code Builder Addon. This addon is available for free for all users with GD Rating System Pro license. And, this addon allows you to get PHP for the various purposes. To display the rating block inside the loop, you can use the ‘Single Rating’ builder.
Let’s say, we want to display stars rating block, with rating disabled, using the default template. In the builder set the Item Selection to Current Post (we are working with WordPress Posts Loop). This builder will generate the code looking like this:
gdrts_posts_render_rating( array( 'echo' => true, 'method' => 'stars-rating' ), array( 'template' => 'default', 'disable_rating' => true ) );
If you place this code inside the WP Loop, you will get normal rating block, with rating disabled. This rating block will show the rating stars, text and logged in user own vote.
But, if you want to show only stars, with no other information, you will need to create a new template and use it in the code builder to get updated PHP snippet code. At the end of this article, you will get the example template in the ZIP archive. To use it:
- Unzip the archive and the singular PHP file place in the active theme folder. More on that, you can find here.
- In WordPress admin side, open Rating System -> Tools -> Recheck and Update panel. This will pick up the new template.
- Revisit the Code Builder, and select the new template instead of the default one. The new template is called ‘Stars Only’.
- Replace the rating PHP snippet code in your theme template loop. The new code will look like this:
gdrts_posts_render_rating( array( 'echo' => true, 'method' => 'stars-rating' ), array( 'template' => 'stars-only', 'disable_rating' => true ) );
For informational purposes, here is the PHP code for the rating template you have available for download.
<?php // GDRTS Template: Stars Only // ?> <div class="<?php gdrts_loop()->render()->classes(); ?>"> <div class="gdrts-inner-wrapper"> <?php do_action('gdrts-template-rating-block-before'); ?> <?php gdrts_loop()->render()->stars(); ?> <?php if (!gdrts_loop()->is_save()) { gdrts_loop()->method()->please_wait(); } gdrts_loop()->json(); do_action('gdrts-template-rating-block-after'); do_action('gdrts-template-rating-rich-snippet'); ?> </div> </div>
As you can see, this template shows only the stars, and if needed the “Please Wait” message if you decide to use this block for rating active block.