Code for Star Ratings in Now Reading Reloaded
To get the starred ratings like you see on my book review and info pages just upload three images to the now-reading folder in your template directory: one representing a no star, one representing a half star, and one representing a full star (I’ve included my images in the download). Then add the following code to your functions.php file.
/** * Rating icons for the library * * @param mixed | $this_book | The id of the desired book; defaults to current book within the `have_books` loop */ function star_rating($this_book = NULL) { global $book; if ($this_book == NULL) $this_book = $book; $rate = $this_book->rating; $no_star = '<img class="star" src="'.get_bloginfo('template_directory').'/now-reading/no-star.png" alt="" width="18" />'; $full_star = '<img class="star" src="'.get_bloginfo('template_directory').'/now-reading/full-star.png" alt="" width="18" />'; $half_star = '<img class="star" src="'.get_bloginfo('template_directory').'/now-reading/half-star.png" alt="" width="18" />'; if ($rate) : switch ($rate) { case 1: echo '<span title="½ star">'.$half_star.$no_star.$no_star.$no_star.$no_star.'</span>'; break; case 2: echo '<span title="1 star">'.$full_star.$no_star.$no_star.$no_star.$no_star.'</span>'; break; case 3: echo '<span title="1½ stars">'.$full_star.$half_star.$no_star.$no_star.$no_star.'</span>'; break; case 4: echo '<span title="2 stars">'.$full_star.$full_star.$no_star.$no_star.$no_star.'</span>'; break; case 5: echo '<span title="2½ stars">'.$full_star.$full_star.$half_star.$no_star.$no_star.'</span>'; break; case 6: echo '<span title="3 stars">'.$full_star.$full_star.$full_star.$no_star.$no_star.'</span>'; break; case 7: echo '<span title="3½ stars">'.$full_star.$full_star.$full_star.$half_star.$no_star.'</span>'; break; case 8: echo '<span title="4 stars">'.$full_star.$full_star.$full_star.$full_star.$no_star.'</span>'; break; case 9: echo '<span title="4½ stars">'.$full_star.$full_star.$full_star.$full_star.$half_star.'</span>'; break; case 10: echo '<span title="5 stars">'.$full_star.$full_star.$full_star.$full_star.$full_star.'</span>'; break; } echo ' '; else : echo "Not Rated"; endif; }
Now add <?php star_rating($book_id); ?>
where you want it to display. If you’re using it inside the have_books
you don’t need the book id. Setting it will override the default, which is the current book. I use a custom field (“Book ID”…creative, I know) to set the book id for the post, and put it in a variable for my templates.
That’s it. You can use any set of images, just use the proper file names or adjust the functions.php code as needed.
Download [download id=”4”]
Thanks! It works perfectly!