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 .= '
Now the output will be added to the two example links. The following HTML syntax should explain it.
Pages
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
7 responses to “Add Links To WordPress List Pages”
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.
Thanks very much for this clear explanation, Frank – just what I needed to learn how to filter wp_list_pages() output.
Please see the codex or the post on on digwp, its great.
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?
@Chris: you can only add the values in the list.
@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.
@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.