Take the 2-minute tour ×

I'm using the wp_list_bookmarks() function in wordpress to get links that have been added in the Wordpress back-end, things like blogroll etc.

My problem is that I need the list to come out in a certain way so that I can put it into columns, with about 5 links in each column.

By default it's just one big list. Is there a way that I can use PHP to alter the way this function posts the links so that I run a counter, and then after 5 links close off the current list and start a new one for the next column?

I basically need something like this:

<ul class="column-1">
<ul class="column-2">
// etc...

Thank you in advance.

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

You can use Wordpress's get_bookmarks() function and then tailor the output to your liking. See here for an example:


$bookmarks = get_bookmarks( array(
            'orderby'        => 'name',
            'order'          => 'ASC',
            'category_name'  => 'Related Sites'

// Loop through each bookmark and print formatted output
$column = 1;
$counter = 1;

echo "<ul class='column-$column'>";
foreach ( $bookmarks as $bm ) { 
    if ($counter >= 5)
        $counter = 1; // reset the counter

        echo "</ul><ul class='column-$column'>";

    printf( '<li><a class="relatedlink" href="%s">%s</a></li>', $bm->link_url, __($bm->link_name) );
echo "</ul>";


Reference: http://codex.wordpress.org/Template_Tags/get_bookmarks#Examples

share|improve this answer
the code is not tested, but I hope you get the idea of what I am trying to propose. So my sincere apologies if there are any bugs (even if I have tried to ensure the code is bug free) :) –  thesocialgeek May 22 '11 at 18:48
Makes sense, I will try it tonight and let you know how it goes. Thanks. :) –  diggersworld May 23 '11 at 12:00
add comment

According to wordpress's Plugin API:

Besides the hooks (actions and filters) described above, another way for a plugin to modify WordPress's behavior is to override WordPress functions. In fact, there is a small set of functions WordPress intends for plugins to redefine.

Unfortunately wp_list_bookmarks isn't on their list of functions that are considered "pluggable".

Maybe you can find something else that will fit your needs in the API?

share|improve this answer
add comment

You can hook into wp_list_bookmarks() from a plugin (that you need to write). That plugin can then manipulate the normal HTML that comes from the wp_list_bookmarks() function and return that manipulated HTML back to it, to be echoed or returned as normal.

The idea is to create (and activate!) a simple plugin like:

Plugin Name: Diggersworld Bookmarks
Plugin URI: http://stackoverflow.com/questions/6089883/can-wordpress-functions-be-modified
Description: Example plugin to customise output from wp_list_bookmarks
Version: 0.0

function diggersworld_list_bookmarks($html)
    // Do your transformation here
    return '<p>Mmm pie.</p>';

add_action('wp_list_bookmarks', 'diggersworld_list_bookmarks');


Save this as a file in your plugins folder, and activate it from within the Wordpress admin panel. You will see that where your bookmarks used to be placed, the Mmm pie. text is there instead. Modify that sample plugin to return your desired HTML using whatever method you like.

Since this is tying into a hook inside the wp_list_bookmarks() function, your templates should call wp_list_bookmarks() as normal.

Of course, the body of the plugin function could use code similar to that in thesocialgeek's answer.

share|improve this answer
add comment

Your Answer


