Adding Additional Links to the Output from wp_list_pages

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> 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!

About Matt

IF YOU REALLY want to hear about it, the first thing you'll probably want to know is where I was born, and what my lousy childhood was like, and how my parents were occupied and all before they had me, and all that David Copperfield kind of crap, but I don't feel like going into it, if you want to know the truth.

In any case I am employed as a Senior Linux Systems Engineer at Rackspace in San Antonio, TX.

I spent the majority of my college years as an Astrophysics major specializing in black holes. When I got to my senior year and was studying 40 hours a week to stay on track I decided to bail and pursue an easier career.

I spend the majority of my time now contributing to WordPress, developing WordPress plugins, helping out in the WordPress IRC Channel, playing football (not American!), practicing Krav Maga, and last but not least, spending time with my Wife and Son.

This entry was posted in Code, PHP, Snippet, WordPress. Bookmark the permalink.

8 Responses to Adding Additional Links to the Output from wp_list_pages

  1. Pingback: Statische Links zu WordPress Seiten hinzufügen - bueltge.de [by:ltge.de]

  2. Pingback: Add Links To WordPress List Pages - This, WordPress, Page, ‘Example, Example, Subpage - WP Engineer

  3. 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

  4. 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.

  5. Pingback: Add Links To WordPress List Pages | Omelyn.com

  6. Pingback: 16 best practise Tipps: statische Seiten in WordPress

  7. Pingback: 20 Tipps zu den statischen Seiten in WordPress

  8. 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

Your email address will not be published. Required fields are marked *

*

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre>