permalink

5

Display Multiple Link Widgets using WordPress Shortcodes

I was working on a theme and wanted to display multiple link widgets (with options like display links from specific categories etc..) in the sidebar, since there was no option in WordPress to do so, I was looking around on the net to display multiple links and luckily found this article on Twitter by SmashingMagazine.com and got to know about WordPress Shortcodes. It was kind of heaven for me, as it was exactly what I wanted..

I started around playing with it and wrote a small piece of code to display multiple links in the widgets.

Here is how I did it…

First of all, add following code in the functions.php file of your theme


add_filter('widget_text', 'do_shortcode');

Above code will allow, WordPress shortcodes to be called from the Widgets.

Now, you need to create a function which will contain the code for the links.


function show_links($atts) {
$show_links = "<ul>".wp_list_bookmarks($atts)."</ul>";
return $show_links;
}

add_shortcode('links', 'show_links');

Here, I have made a call to wp_list_bookmarks function and added shortcode called “links” for the same. I have customized the code according to my need. I am basically wrapping the list in <ul>. (Adding UL’s HTML code in the widget bar doesn’t seem to be working fine)

Once this is done, save the file, logon to your WordPress admin panel, click on Widgets under themes, add a text widget and call your recently created shortcode:


[links title_li="" categorize="0" category="2" echo="0"]

In above shortcode, I am setting the title as blank, calling the links from the category ID 2 and setting the echo to 0, so that my wp_list_bookmarks does not echo the data (The data will be echoed from the variable show_links)

I guess, that’s about it.. I am sure, there might be better way to achieve the same results but at this point of time, I found this pretty simple, so implemented it.

I hope it helps 🙂

Also, do let me know, if you feel the code can be improved using different methods.

5 Comments

  1. Nopes not here, implemented it on one of my clients’ site theme.. work is still going on so can’t show the link

  2. Pingback: Frische-zitronen.de | Webdesign | Webdesign - in Wordpress auch mit fertigen Templates

  3. Thank you! This is *exactly* what I was looking for and your tutorial is so clear. I can’t tell you how much I appreciate this!

Leave a Reply

Required fields are marked *.


This site uses Akismet to reduce spam. Learn how your comment data is processed.