Read my latest blog entry: Does your web site help, or hurt your business?.
Subscribe to my RSS Feed or by Email.
  • Home
  • About
  • Services
  • Blog
  • Contact
Christopher Ross Building better web sites, one pixel at a time.

Show Both Links and Pictures in WordPress wP_list_bookmarks()


Welcome to my blog, please feel free to subscribe to my RSS feed or leave a comment.

As some of you may have noticed, I updated the design for my site this week. It was a pretty major overhaul of the basic code, although it is still 100% WordPress. The upgrade that I’ve had the most messages about is my links list at the bottom of the page. It uses the standard WordPress Links list with a few minor scripting changes.

By default, WordPress wp_list_bookmarks() will display either the graphic or the title of your link but not both. If there is a graphic image set, it will simply ignore the title and output only the graphic. If no graphic is present, it will only output the text. Not exactly what I wanted.

What I wanted was what Jeremy had. So I set about trying to find the code to do this in WordPress only to find out that it simply didn’t exist. What I did find was a lot of people grumbling about it, so  I fixed the problem using WordPress’s built in get_bookmarks() function.

Here’s what I did:

<strong>What I’m Reading</strong>
<ul><?php 

$linklist = get_bookmarks(’category=70′); 
foreach ($linklist as $site) { 
echo “<li>”;
if (strlen($site->link_image)>2) {
echo “<span class=’linkimage’>”;

echo “<a href=’”.$site->link_url.”‘ title=’”.$site->link_description.”‘ rel=’me’><img src=’”.$site->link_image.”‘ alt=’”.$site->link_name.”‘/></a>”;
echo “</span>”;

} else {
echo “<span class=’linkimage’></span>”;
}

echo “<span class=’linkname’>”;
echo “<a href=’”.$site->link_url.”‘ title=’”.$site->link_name.”‘ rel=’me’>”.$site->link_name.”</a>”;
echo “</span>”;
echo “</li>\n\r”;
}
?></ul>

For those of you who know PHP what I did was pretty simple but for the rest of you, here’s the basic breakdown:

$linklist = get_bookmarks(’category=70′); 

Get the values of category #70 (I only wanted those links) and load them into a variable.

foreach ($linklist as $site) { … }

Loop through the variable.

if (strlen($site->link_image)>2) { … }  else { … }

If the link_image value (that’s where the image is stored) has a value of more than two characters, do this. Otherwise, do something else. For the record, sometimes the value returned a false positive when testing for isset() so I choose to do a strlen() instead.

echo “<span class=’linkimage’>”;
echo “<a href=’”.$site->link_url.”‘ title=’”.

$site->link_description.”‘ rel=’me’><img src=’”.$site->link_image.”‘ alt=’”.$site->link_name.”‘/></a>”;
echo “</span>”;

The code above displays a <span> tag. I repeat the process if there’s no image found using an empty <span> tag to keep the text alignment appropriate.

echo “<span class=’linkname’>”;
echo “<a href=’”.$site->link_url.”‘ title=’”.$site->link_name.”‘ rel=’me’>”.$site->link_name.”</a>”;
echo “</span>”;
echo “</li>\n\r”;

Finally, I display another <span> called linkname which stored the text based hyperlink to the site. 

That’s all she (actually he) wrote … do you have any suggestions how to improve this code? Let me know and I’ll owe you a pint. Also, if you use this code please post a link to your web site below so that I can see it in action. Thanks.

Other Posts of Interest

  • Automatically Title Casing Titles in WordPress
  • Adapting my holiday logo rotator for WordPress
  • How to rotate your logo on holidays.
  • Blog Roundup for 5th of October 2008
  • Sick Riot Photography
  • Blog Roundup for the 6th of September 2008
  • Blog Roundup for the 30th of April 2007
  • Blog Roundup for the 4th of March 2006
  • How do you install WordPress Plugins?
  • Fundraising Thermometer Plugin for Wordpress
Posted on: Wednesday, October 22nd, 2008
Tags: links, php, tutorial, WordPress
Posted in WordPress | | Read more

Did you find this article useful? You're welcome to post a link to this website along with the title but please don't copy the whole article. You can also link back using the following code:

<a http://www.thisismyurl.com/wordpress/show-both-links-and-pictures-in-wordpress-wp_list_bookmarks/" rel="bookmark" title="Show Both Links and Pictures in WordPress wP_list_bookmarks()">Show Both Links and Pictures in WordPress wP_list_bookmarks()</a>

About the Author

Christopher Ross About the author.
Thanks for taking the time to read my blog. If you don't already know me, my name is Christopher Ross and I'm a web evangelist living in Fredericton Canada. When I'm not writing about the Internet, I'm helping small businesses improve their search engine performance or building better WordPress web sites for clients.

Please feel free to leave a comment below, or contact me if you have any questions.

3 Comments

|
comments rss [?] | trackback uri [?]

3 Responses to “Show Both Links and Pictures in WordPress wP_list_bookmarks()”

  1. savings account Says:
    October 23rd, 2008 at 11:16 am

    You seem to be very good with this. I need to outsource a blog build for a friend, very limited budget but guess you can help us.

    Thanks

  2. Pingback: Free WordPress Theme - Pink Flowers :: Christopher Ross
  3. Keith Says:
    December 8th, 2008 at 4:01 am

    Works like a charm!

    Thank you

Leave a Reply

HBO
iTunes The Office
LEGO - Clone Wars
BlueHost
Fujitsu
Fujitsu
iTunes
iTunes Sarah Marshal
Total Training DVDs
LEGO - Agents
HBO
Total Training DVDs
Recent Comments
  • Electric cars: Very nice post and I gain much more info from this post about, “How to useful my site for my...
  • Internet Marketing Strategies: That is really bad business, with the Internet making the world such a small place,...
  • Adam Love: wtf, that was prety blunt don’t you think?
  • George Matthews: well that’s effective, know of an easy way of title casing static sites, other than...
  • Market Secrets Blogger: At first this one click plugin install worked just fine but now I get the following error:...
  • Rodney@Blogging with WordPress: Nice work Christopher - not too shabby. You must have some pretty good backlinks....
  • david forex trading online: Thank you for letting it slide. I know it is annoying when people use keywords as their...
  • Ikki at SEO Blog: As Mr. forex trading online (I hate it when people don’t put their names on the Name...
  • David: One more strategy of search engine marketing is to create a buzz about your site in the internet. This can be...
  • Internet Marketing Strategies: Nice tutorial! JR Internet Marketing Strategies’s last blog post..Blogging Goodies...
Top Commentators
  • rv for sale (8)
  • Internet Marketing Strate... (7)
  • Electric cars (6)
  • Ikki at SEO Blog (4)
  • forex trading online (4)
  • RNB Love Songs (4)
  • Make Money Online (3)
  • Mike Collins (2)
  • PizzaForADream (2)
  • Webkinz (2)
© Copyright 2009 Christopher Ross. All rights reserved.
  • Follow my posts:
  • RSS
  • Comments
  • Email
  • Other:
  • Privacy