Adding Additional Links to the Output from wp_list_pages

Code, PHP, Snippet, Wordpress 2 March 2009 3 Comments

One common question in the WordPress IRC Channel is how to add external links, or links to content other than pages, to the output of wp_list_pages, usually not that exactly but that is what they mean. You can of course modify your theme by inserting <li><a href="http://example.org">Example.org</a></li> <p> immediately following the wp_list_pages function however, this is not always the best solution. In my case I am working on developing a theme for distribution and testing it in the best way possible by running it as my main theme on my site. Since I want extra links to appear there and I don’t want to have content in the theme that will not be distributed to users, filtering the output of wp_list_pages works wonderfully.

You can put this following code in your themes functions.php, or in a plugin. Since I am trying to keep from modifying my theme, a plugin is the better option.

add_filter('wp_list_pages', 'add_forum_link');
function add_forum_link($output) {
        $output .= '
<li><a href="http://forum.example.org/">Forum</a></li>

';
        return $output;
}

The above code will add a link to a forum at http://forum.example.org/.

Enjoy!

3 Responses on “Adding Additional Links to the Output from wp_list_pages”

  1. Chukki.de says:

    Nice code snippet…but do you have an idea how i can add a “news” link between two other pages?
    I thought about a solution with str_replace… but maybe there is an easier way?

    greets chukki

  2. Chukki.de says:

    It’s me again :)
    I solved it in a way like this:

    add_filter(‘wp_list_pages’, ‘add_home_link’);
    function add_home_link($output) {
    $output = str_replace(”,’News\n’, $output);
    return $output;
    }

    It works very good. I searched for the position where i want to add the link and put it before it.

  3. Exactly what I was looking for. I needed to add a link to a post category on my header and this worked great! Thanks!

Leave a Reply