How to Display Ratings with WordPress Links

How to Display Ratings with WordPress Links

If you’re like me, the WordPress Codex is an endless source of new template options. Today we’re going to use an often overlooked feature in the WordPress dashboard to assign and display ratings on links.

The aim of this tutorial is to create a rating display system that pulls from the link section of WordPress (not posts). The rating will be represented on a five star scale to help add some flair to the result. Hopefully this will bring some much deserved attention to your favorite links, and let readers know which ones are worth the time.

The Result

Here’s a look at the final product live in a sidebar:

Wordpress Ratings on Links Result

The nice part about this is that once we’ve inserted the code into a template, everything can be managed from fields inside the WordPress dashboard. Updating or adding a link is as simple as filling out a few text boxes in the WordPress option panel.

Adding Ratings in the Dashboard

First, it would probably be a good idea to see where the you can assign ratings to links in the first place. Under the WordPress dashboard, navigate over to the links panel and then select “Edit” on any of the links there.

Rating Box Location

If you scroll all the way to the bottom of this page, you should see that the final option (under the “Advanced Box”) is a select box entitled “Rating”. This will be the basis for this tutorial.

Template Tags & PHP

Before we continue, let’s take a look at the individual link result to get a better idea of what we’ll be doing:

Wordpress Link Ratings Breakdown

As you can see, we’re pulling four different fields together to compose each link item. Each of these fields will be contained within a PHP object for the link roll that we’ll define below.

This code is loosely based off of a thread found in the WordPress support forum. It’s been adapted for use with ratings and a few other fields for this tutorial. Copy this into your WordPress template where you would like the links and ratings to appear (e.g. sidebar.php).

Typically I like to avoid throwing everything out there at once in tutorials, but in this case the PHP and HTML are so closely related that splitting it up would likely be more confusing. To help prevent confusion an explanation will follow down below.

This code starts by defining $bookmarks as an array, which will be populated with the list of links from WordPress. In the arguments of the get_bookmarks() method, we’re saying we want the results ordered by the value of the rating in descending order. This way the “best” links will be displayed at the top of the list. If you need additional organization happening at this stage, the WordPress Codex has an entry listing all other arguments available.

Adding a Star Rating System

Let’s make the ratings a little more exciting by showing a star ranking system. For this tutorial we’ll be working on a five star system in full intervals. Implementing half stars is certainly a possible expansion to this, but for most bloggers a five star system should be accurate enough. For that sake of simplicity, we’ll assume that any rating over 5 also denotes a 5 star rating.

The star icon used is from a WeFunction set available for free here. It is named star_48.png once extracted. You will need to place it into a folder named “images” located the root directory of your theme to follow this example along, but feel free to substitute as needed.

Within the link loop (directly under the bookmark description line) paste in the following PHP to include star ratings.

Sample CSS

If you’re looking for a starting point to style the list you’ve just created, here’s the CSS I’ve used for my demonstration. One of the biggest changes below is to the size of each star image.

Rate Away

Hopefully by now you should have a much more visual way of displaying link ratings. To help solve any issues, I’ve provided a downloadable version below of the PHP block (minus CSS) to use as reference.

Have any interesting ideas for further application with this feature? Have I missed something? Be sure to let us know in the comments below!

Posted Tuesday, July 28th, 2009 · Back to Top

SPONSOR

Add Comment

18 Comments 11 Mentions

  1. Khan Author Editor

    Another good post, found it to be very useful. A good theme developer should avoid dependence on plugins as much as possible.
    .-= Khan´s last blog ..List of Really Useful Plugins and Resources For MooTools =-.

    ·

  2. Sasan Author Editor

    Thanks!
    I’ll used the one you made …

    ·

  3. Stefson Author Editor

    Any way this can be implemented into a regular post? Not a blogroll?

    ·

  4. TaiPhanMem.org Author Editor

    Good post. Thank you very much.
    .-= TaiPhanMem.org´s last blog ..MoveOnBoot – Xóa, đổi tên, di chuyển file, thư mục bị khóa khi khởi động lại =-.

    ·

  5. Alexis Chatougri Author Editor

    Thank you very much for this tip ! Exactly what I was looking for :-P

    ·

  6. david w Author Editor

    Wow this is just what I need on my site. I need a link rating tool. Only problem is im not that familiar with code. Can anyone please tell me how, , , no not how I know how…. but where exactly within sidebar.php to paste these 2 sets of code?

    ·

  7. George Author Editor

    Thanks very much for this, been looking how to add ratings for ages!!

    ·

  8. JG Ballard Author Editor

    Adding ratings is so useful, thank you!

    ·

  9. Cody Burleson Author Editor

    Thanks! My links page now supports link images, link categories, and thanks to you, no link ratings!

    http://www.codyburleson.com/links/

    Here’s the code for my whole bookmarks rendering routine in case anybody wants to do something similar (I hope the code is going to paste OK):

    count > 0) {
    echo ” . $term->name . ‘ ‘;
    /* print_r($term); */
    /* was orderby ‘name’ ‘ASC’ */
    $bookmark_args = array(
    ‘orderby’ => ‘rating’,
    ‘order’ => ‘DESC’,
    ‘limit’ => -1,
    ‘category’ => $term->term_id,
    ‘hide_invisible’ => 1,
    ‘show_updated’ => 0
    );

    /*
    * Returns list of bookmark row objects. Each bookmark object may contain the following:
    * ‘link_id’,
    * ‘link_url’,
    * ‘link_name’,
    * ‘link_image’,
    * ‘link_target’,
    * ‘link_category’,
    * ‘link_description’,
    * ‘link_visible’,
    * ‘link_owner’,
    * ‘link_rating’,
    * ‘link_updated’,
    * ‘link_rel’,
    * ‘link_notes’,
    * ‘link_rss’
    */
    $bookmarks = get_bookmarks($bookmark_args);

    /* if( !empty($bookmarks) ) { */
    $str = ”;
    foreach ( $bookmarks as $bm ) {
    $str .= ”;

    if(!empty($bm->link_image) ){
    $str .= ‘link_target . ‘” class=”relatedlink” href=”‘ . $bm->link_url . ‘”>’ . ‘link_image . ‘” alt=”” align=”left” border=”0″/>’ . ‘‘;
    }

    $str .= ‘link_target . ‘” class=”relatedlink” href=”‘ . $bm->link_url . ‘”>’ . $bm->link_name . ‘‘;

    /* START: STAR RATINGS */

    if(!empty($bm->link_rating) ){

    $link_rating = $bm->link_rating;
    $str .= ”;
    if ($link_rating >= 5){

    //If rating is five or greater, just assign a perfect score
    for ($i = 1; $i < 6; $i++){
    $str .= '’;
    }

    } else {
    //If rating is under 5, display same number of stars.
    for ($i = 1; $i <= $link_rating; $i++){
    $str .= '’;
    }
    }
    $str .= ”;
    }

    /* END: STAR RATINGS */

    if(!empty($bm->link_description) ){
    $str .= ” . $bm->link_description . ”;
    }

    }
    $str .= “”;
    echo $str;
    /* } */

    }
    }
    }
    /* END: RENDER CATEGORIZED BOOKMARKS */
    ?>

    ·

  10. Cody Burleson Author Editor

    Ignore the code above, it was a bad paste! Trying again with a pre tag:

    ·

  11. Valentine Author Editor

    Although the description is very detailed, i still can’t catch the idea.

    ·

  12. Roger Vivier Chaussures Author Editor

    Roger Vivier Chaussures

    ·

  13. indra putra bungsu Author Editor

    a very useful tutorial. I will try to use it..

    ·

  14. Leonie Author Editor

    I’m looking for something similar, though I don’t want to rate links, but just items in a list. Can anyone help me with that?

    ·

  15. Carol Author Editor

    Does anyone know of a way to allow users to rate the links? I’m looking for a way to determine which links are most helpful to visitors to my site. Obviously, if I’m rating the links myself, that’s not going to work for my purpose.

    ·

  16. AMCT INSTITUTE Author Editor

    Very impressive content..Keep it up..

    ·

  17. social signals seller Author Editor

    You can certainly see your expertise in the article you write.
    The sector hopes for even more passionate writers such as you who are not afraid to
    say how they believe. All the time go after your heart.

    ·

  18. http://www.scribd.com/doc/156664283/Discover-the-Reality-About-Inexpensive-Present-Cards Author Editor

    Woah! I’m really enjoying the template/theme of this
    blog. It’s simple, yet effective. A lot of times it’s hard to get that “perfect balance” between usability and visual appearance.
    I must say you have done a very good job with this.
    In addition, the blog loads very fast for me on Chrome.
    Exceptional Blog!

    ·

 

Build Internet by One Mighty Roar. Since 2008.