Displaying a random blogroll link
A function to fetch and display a random blogroll link in your Wordpress Theme.
TweetWe are going to make a simple function in WordPress to fetch a random blogroll from the WP database and display it as a link.
Place the following lines of code in your theme’s function.php file.
1 2 3 4 5 6 7 8 9 10 11 12 | function random_blogroll($number=1) { global $wpdb; $query = "SELECT link_url, link_name, link_description FROM $wpdb->links WHERE link_visible = 'Y' ORDER BY rand() LIMIT ".$number; $results = $wpdb->get_results($query); foreach ($results as $result) { $link_url = $result->link_url; $link_name = $result->link_name; $link_description = $result->link_description; $retsrting .= '<a href="'.$link_url.'" rel="external" title="'.$link_name.'">'.$link_name.'</a> '; } return $retsrting; } |
To display the random link in your blog, place the following code where you want the link(s) to appear:
1 |
To display ‘x’ number of links, pass the number as a parameter to the function
1 2 | //Example - To display 4 Blogroll links <? if(function_exists('random_blogroll')) echo random_blogroll(4); ?> |
Was this post useful? Share on Twitter or Facebook and spread the love.
Related Posts
- Make URLs within posts clickable
- How to remove the WordPress Admin Bar
- Fix “Is its parent directory writable by the server?” error for WordPress Image uploads
Below is a list of possibly similar posts, as suggested by six huge golems hired to do just that.
Now What?
- Tweet this It's been long since you tweeted
- Ask Got a query you want us to answer?
- Spread the love Share it socially
Finished reading? Wondering what to do? Heres a hint. Here are some suggestions just for you