Blog

iWeb Open Source

Customising WordPress wp_list_pages()

January 10, 2012 | Gareth

I recently worked on a project that required some html customisation of the wp_list_pages() function. In order to style the list I needed to wrap a <span> tag around the existing link text inside the function. Here is the complete block of code I used:

<?php
    $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
    if ($children) {
?>
<ul class="sub-nav">
    <?php
        $pattern = '/href=\"(.*)\"\>/';
        $children = preg_replace($pattern, 'href="${1}"><span>', $children);
        echo str_replace('</a>', '</span></a>', $children);
    ?>
</ul>
<?php } ?>

This code produces the following html output:

<ul class="sub-nav">
	<li><a href="/about/"><span>About Us</span></a></li>
</ul>

It works by looking for the href parameter and replaces it with our custom href which includes the opening <span>.

If you are looking for award winning Web Design give iweb a call today!

Tags:
Categories:
WordPress