Useful WordPress navigation code

For most of my WordPress websites I tend to use an old but superb plugin called “Fold Page List” – this improves the standard WordPress navigation by showing child pages only when in a particular section! Perfect!

But what if I didn’t want to show the “whole navigation” on the left hand side when I went into section?!?!  Well as far as I can see I needed to create/use something custom.

So a search on Google came up with a few results but nothing really seemed to really do what I wanted, until I came across this post from TammyHart Designs – and it worked out perfect!

Made a few tiny tweaks to make sure the code validated so here’s  my reworked code (kudos to TommyHart for the original start though!):

<?php if ( is_page() ) { ?>
            <?php
                if($post->post_parent)
                    $children = wp_list_pages('title_li=&child_of='.$post->post_parent.'&echo=0'); else
                    $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
                    if ($children) { ?>

                    <h2><?php $parent_title = get_the_title($post->post_parent); echo $parent_title; ?></h2>
                    <ul>
                        <?php echo $children; ?>
                    </ul>
            <?php } ?>
        <?php } ?>

Still not perfect as I only want to show grand-children pages when you click on the child page – exactly what Fold Page list does – so looks like I’ll be doing a tweak to that involving the above really soon 😉

Leave a Reply