Build Internet has a brand new theme, and that's only the beginning. Read the full story or hide this bar

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.

<?php

$bookmarks = array();
$bookmarks = get_bookmarks(array(
		orderby => 'rating',
		order => 'DESC'
));

if ($bookmarks[0] != '') { ?>
	<ul class="linklist">
		<h3>Recommended Links</h3>
		<?php foreach ( $bookmarks as $bookmark ) { ?>
				<li>
					<h4><a title="<?php echo $bookmark->link_name; ?>" href="<?php echo clean_url($bookmark->link_url); ?>" target="_blank"><?php echo $bookmark->link_name; ?></a></h4>
					<p><?php echo $bookmark->link_description ?></p>
				</li>
		<? } ?>
	</ul>
<?php } ?>

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.

<div class="link-rating">
	<?php
		$link_rating = $bookmark->link_rating;
		if ($link_rating >= 5){
			//If rating is five or greater, just assign a perfect score
			for ($i = 1; $i < 6; $i++){ ?>
			      <img src="<?php bloginfo('template_directory'); ?>/images/star_48.png"/>
			<?php }
		}else{
			//If rating is under 5, display same number of stars.
			for ($i = 1; $i <= $link_rating; $i++){ ?>
				<img src="<?php bloginfo('template_directory'); ?>/images/star_48.png"/>
		<?php }
	} ?>
</div>

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.

/*Link Rating*/
.linklist{clear:both; font-family:arial, sans-serif;}
	.linklist li{clear:both; overflow:hidden; display:block; margin-bottom:5px; padding:10px; background:#F3F3F3; font-size:12px; border:1px solid #DDD;}
	.linklist h3{margin-bottom:10px;}
	.linklist h4{clear:both; font-weight:bold; font-size:16px;}
		.linklist h4 a{text-decoration:none; color:#191919;}
	ul.linklist li p{padding-bottom:5px; margin:5px 0px; padding:0; color:#333;}
.link-rating{clear:both; margin:0;}
	.link-rating img{width:20px; height:20px; margin:0; padding:0;}

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!

Wordpress.com stats not installed! Posted Tuesday, July 28th, 2009 / Back to Top

I this post. Tweet
SPONSOR

12 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 =-.

    July 28, 2009 · Reply

  2. Sasan Author Editor

    Thanks!
    I’ll used the one you made …

    July 31, 2009 · Reply

  3. Stefson Author Editor

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

    August 1, 2009 · Reply

  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 =-.

    August 2, 2009 · Reply

  5. Alexis Chatougri Author Editor

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

    December 10, 2009 · Reply

  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?

    December 16, 2009 · Reply

  7. George Author Editor

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

    December 25, 2009 · Reply

  8. JG Ballard Author Editor

    Adding ratings is so useful, thank you!

    February 19, 2010 · Reply

  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 */
    ?>

    June 9, 2011 · Reply

  10. Cody Burleson Author Editor

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

    /* START: RENDER CATEGORIZED BOOKMARKS */
    $taxonomy = 'link_category';
    $args ='';
    $terms = get_terms( $taxonomy, $args );
    
    if ($terms) {
    	foreach($terms as $term) {
    		if ($term->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 */
    

    June 9, 2011 · Reply

  11. Valentine Author Editor

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

    February 21, 2012 · Reply

  12. Roger Vivier Chaussures Author Editor

    Roger Vivier Chaussures

    March 3, 2012 · Reply

 

Join the Conversation

Back to Top / Comment RSS

2012 Build Internet. Created by One Mighty Roar. Icons by Komodo Media. Back to Top