I’ve been trying to work with Conditional Tags and cannot wrap my head around this problem. PHP novice here.
On sub-pages I need to display the parent page title as well as the page title. I have it working with this:
<h1><?php echo get_the_title($post->post_parent);?></h1>
<h2><?php the_title(); ?></h2>
But the problem I have now is that on the parent pages the page title is displayed twice, as the parent page title and the page title. Instead, when on a parent page, I need the h2
to display “Select a sub-page”, if there are child pages…or display nothing, if there are no child pages. Something like this is what I’m thinking is possible:
<h1><?php echo get_the_title($post->post_parent);?></h1>
<h2>
<?php
if is_parent_page_without_children() {echo '';} ;
elseif is_parent_page_with_children() {echo 'select a sub-page';} ;
else the_title();
?>
</h2>
3 Answers
Based on the comment exchange, here’s what I think you’re after:
<h1><?php echo get_the_title( $post->post_parent ? $post->post_parent : $post->ID ) ?></h1>
<?php if ( $list = wp_list_pages( "echo=0&child_of=$post->ID" ) ) : ?>
<h2>Select a sub-page</h2>
<ul>
<?php echo $list ?>
</ul>
<?php elseif ( $post->parent ) : ?>
<h2><?php the_title() ?></h2>
<?php endif ?>