Add Links To WordPress List Pages

Not infrequently, the menus on blogs, created with WordPress, having static links. This may initially seem easier and less trouble, but if changes or maintenance by users who have no knowledge or access to the templates occur, it is cumbersome and error prone. Therefore I prefer to use the necessary template tags and using the help of parameters that are not desirable (exclude) or desired (include) pages load. In addition, you can add content via the Hook, which I will explain briefly.


Like many features of WordPress, you can also use a filter hook for wp_list_pages(). The function itself has a detailed description in the Codex.

$output = apply_filters('wp_list_pages', $output);

Let's use this advantage and expand the content of two sample links that are otherwise manually added in the theme. We place the following code snippet into the functions.php of the theme. The tags need to be passed as the hook is integrated after the list.

if ( !function_exists('fb_add_page_link') ) {
	function fb_add_page_link($output) {
		$output .= '<li><a href="http://example.com/1/">Example 1</a></li>';
		$output .= '<li><a href="http://example.com/2/"><span>Example</span> 2</a></li>';
	
		return $output;
	}
	
	add_filter('wp_list_pages', 'fb_add_page_link');
}

Now the output will be added to the two example links. The following HTML syntax should explain it.

<li class="pagenav">
	<h3>Pages</h3>
	<ul>
		<li class="page_item page-item-2"><a href="http://example.com/?page_id=2" title="About">About</a></li>
		<li class="page_item page-item-64"><a href="http://example.com/?page_id=64" title="Default Form">Default Form</a></li>
		<li class="page_item page-item-40"><a href="http://example.com/?page_id=40" title="Offerings">Offerings</a></li>
		<li class="page_item page-item-12"><a href="http://example.com/?page_id=12" title="Page 2">Page 2</a>
			<ul>
				<li class="page_item page-item-14"><a href="http://example.com/?page_id=14" title="Subpage">Subpage</a>
					<ul>
						<li class="page_item page-item-45"><a href="http://example.com/?page_id=45" title="Subsubpage">Subsubpage</a></li>
					</ul>
				</li>
			</ul>
		</li>
		<li class="page_item page-item-16"><a href="http://example.com/?page_id=16" title="Page 3">Page 3</a></li>
	</ul>
</li>

<li><a href="http://example.com/1/">Example 1</a></li>
<li><a href="http://example.com/2/"><span>Example</span> 2</a></li>

Be careful, these links will be added after a check of the parameters and are therefore no longer able to be removed with the help of the parameter. Assuming you use the template tag wp_list_pages() several times with different output, there will be for all the expansion included. Differences can not easily be made. Not even with the second template tag for pages wp_page_menu(). This template tag drags the content of the function wp_list_pages()!

As an indication it should be mentioned that the template tag wp_page_menu() can be used only since WordPress version 2.7. This tag may in fact can not much more, except automatic adding a Home link and the control of the output (echo), more in description. But the function differentiate still in two other cases so you can hook in at two points via the filter hook and modify.
For one, you can extend the arguments ...

$args = apply_filters( 'wp_page_menu_args', $args );

... and on the other hand you can extend the menu.

$menu = apply_filters( 'wp_page_menu', $menu, $args );

And so you can once again realize different outputs. The following syntax calls the example links in the latter function wp_page_menu(), and thus manages wp_list_pages() without the extension of the two links.

add_filter('wp_page_menu', 'fb_add_page_link');

With a little reflection you can be quite flexible to configure output and the authors can participate when it comes to automatically add pages to navigations.

I have noticed the idea at Sivel.net

Comments are closed.

7 comments

  1. James John Malcolm

    I think it's better to keep the links in a "html" widget, and stick that under the menu.

    That keeps link management in the admin area and out of the templates.

  2. JohnMiller

    Thanks very much for this clear explanation, Frank - just what I needed to learn how to filter wp_list_pages() output.

  3. Frank

    Please see the codex or the post on on digwp, its great.

  4. Chris

    Thanks for the post, it was just what I was looking for! By chance, is it possible to control where the additional links show up in the array? Or are they always added to the end?

  5. Frank

    @Chris: you can only add the values in the list.

  6. Jeff

    @Frank: So is that a no? I'm having the same issue as Chris- trying to add a link to the front of wp_list_pages and can't seem to do so.

  7. Frank

    @Jeff: you have right. THe example-function add only the pages. You must change the function and save the $output and add $output after your new sites.