Wordpress Show Parent Title if Showing Children Pages
If your using wordress and you would like to show the child titles along with the parent page then this is for you. If you are using wordpress for a corporate website (wordpress as a CMS) sometimes you want the have a sidebar that list the other pages in that category. If children, parent title, if not, No title! If it’s your main title is email marketing you might have a list of sub pages such as
Articles & Guides
Merge Tag Cheatsheet
Email Marketing Stats
Template Language
HTML Email Templates
etc
You only want the list of sub pages to show up under the parent page email marketing or under the sub pages of that same group. Add the following code to your sidebar. Great thing about the code is if your parent page does not have any children it won’t show anything. Also when the end user or client is update the website if they add a page or change a title then it will automatically show up.
<?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) { ?>
<h3>
<?php
$parent_title = get_the_title($post->post_parent);
echo $parent_title;
?>
</h3>
<ul>
<?php echo $children; ?>
</ul>
<?php } ?>
People who read this also read:



