WordPress: How to dynamically modify the output of the wp_list_pages

Posted on: 02.24.09

Modifying the output of Wordpres’ template tag wp_list_pages

I’ve noticed a lot of traffic to my previous post on this subject: How to style wp_list_pages, which means that there is a lot of interest in this subject.
I recommend reading the previous article as it might explain certain things which I am going to write about here.

For the template that I was recently working on, I needed a modified output of the default <?php wp_list_pages(‘arguments’); ?> tag, in a way that it would change the link into and anchor. For example, instead of this:

<li class="page_item page-item-1"> 
    <a title="thisisanexample" href="http://www.thisisanexample.com">this is an example</a>
</li>

into this:

<li class="page_item page-item-1"> 
    <a title="thisisanexample" href="#thisisanexample">this is an example</a>
</li>


If you are wondering what are anchors for: these are the shortcuts that take you from one part of a page to another part of the same page.
Here’s what I had to start with:

So, this is how to do it…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
    $my_pages = wp_list_pages('echo=0&title_li=&child_of=5&depth=1');
    $pieces = explode('"', $my_pages);
    $i = 5;
    $j = 3;
    $limit = count($pieces);
    for (;$i<$limit;) {
        $tmp1 = '#'.$pieces[$j];
        $pieces[$i] = $tmp1;
        $i = $i+6;
        $j = $j+6;
    } 
    $tmp2 = implode('"',$pieces);
    echo $tmp2;
?>

from:

$my_pages = '<li class="page_item page-item-1"><a title="example title" href="http://www.examplelink.com">example title</a></li>'

into:

pieces[0] = "<li class="
pieces[1] = "page_item page-item-1"
pieces[2] = "><a title="
pieces[3] = "example title"
pieces[4] = " href="
pieces[5] = "http://www.examplelink.com"
pieces[6] = ">example title</a></li>"
$limit = count($pieces);
$tmp1 = '#'.$pieces[$j];
$pieces[$i] = $tmp1;

And that is it.

It might happen that the wp_list_pages function outputs the title and the link swapped (link comes before the title), then just swap the values of $i and $j, and it should work.

Oh, and it only works with the unmodified output of the <?php wp_list_pages(‘arguments’); ?> tag – it might behave differently when used together with what I have discussed in the previous link on this subject: How to style wp_list_pages

As usual, let me know what you think about this hack (especially since it isn’t the only way of doing it) and ask questions in the comments if you need help.

Thanks
If you found this interesting, please also check out this post on styling wp_list_pages

 
Post Category:
 
 

Comments (2)

Ok, this is excellent, and almost does exactly what I’m after. That is, how can you add a class to a specific li within the ul.
Perhaps the first item in the list, or the last, or one (or more) randomly within the list itself?
Any ideas on how you might achieve this?
Great article(s) though – impressive.
:)

Jason added these words on May 21 09 at 05:37

Thanks Jason!
I would try to get the output of the wp_page_list() into a variable, then split it at each < li > and place it into an array, and do a for() loop on the array, changing the required array data (or just change the required array position, without doing a loop). After that you would have to merge the array back into a variable and output it. Hope this will get you started.
Let me know how it works for you.
All the best,
Maciek

[EDIT] sorry for the mess with the missing text

Maciej Wantusiak added these words on May 21 09 at 08:24
Add a Comment




XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">