Rating list loop with active ratings

GD Rating System includes own rating list query object that works similar to the WordPress WP_Query, but for rating data. Using this query object you can construct your own rating loop and show ratings.

This example will construct the query loop and show active rating blocks inside the loop. To see the supported allowed arguments for the query object, check out the previous article: Rating query object.

Here is a complete example:

<?php

$args = array(
    'method' => 'stars-rating',
    'entity' => 'posts',
    'name' => 'post',
    'limit' => 10
);

$rating_query = gdrts_query()->run($args);

if (!empty($rating_query)) {

?>

<table class="gdrts-ratings-grid">
    <thead>
      <tr>
      <td style="width: 10%;"> </td>
      <td style="width: 50%;">Post</td>
      <td style="width: 40%;">Rating</td>
    </tr>
  </thead>
  <tbody>

<?php

foreach ($rating_query as $item) {

  ?>

    <tr>
      <td><?php echo $item->ordinal; ?></td>
      <td><a href="<?php echo $item->data->get_url(); ?>">
            <?php echo $item->data->get_title(); ?></a></td>
      <td><?php

      gdrts_render_rating(array(
        'echo' => true, 
        'item_id' => $item->item_id,
        'method' => $args['method']
      ), array(
        'template' => 'light',
        'style_size' => 32,
        'style_class' => 'gdrts-custom-stars-query'
      ));

      ?></td>
    </tr>

  <?php

}

?>

  </tbody>
</table>

<?php

}

?>

We start by defining filtering arguments, this example shows top 10 posts ordered by rating, for stars rating method.

On line 10, we are getting the query object and running it with our arguments. On line 12, we check if there are any results, and if there are, from line 28 we stars the FOREACH loop to go through all the rating items. Lines 16 to 24 define the header of the table.

Lines from 32 to 48 are displaying each rating item as the line in the the table. Line 37 uses gdrts_render_rating function to display rating block. This block uses same method as we have used in the initial $args variable. Display of the block is done using ‘light’ template, and stars size is set to 32px.

1
0
134
Rate this article

You are not allowed to rate this post.

Comments

Leave a Reply