2

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>
1

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 ?>
  • Ah, I see now. But I'm not needing to display the list of pages, so I've altered line 2 to just <?php if ( wp_list_pages( "echo=0&child_of=$post->ID" ) ) : ?> but the page title is not displaying on sub-pages – codeview Apr 17 '14 at 17:34
  • How deep is your page hierarchy? i.e. are their children who are also parents? – TheDeadMedic Apr 17 '14 at 17:43
  • There may be grandchildren in the future, but right now it's just parents and children (2 levels of pages) – codeview Apr 17 '14 at 18:07
  • @TheDeadMedic I really don't think it is wise to base this condition on string comparison of wp_list_pages output. There is wp_list_pages filter, so any plugin/theme may change this output and break this condition. – Krzysiek Dróżdż Apr 17 '14 at 18:17
  • I did not think wp_list_page was necessary to accomplish this but both yours and @KrzysiekDróżdż answers were utilizing it and it seemed to make sense...just a bit more complicated than I needed. – codeview Apr 17 '14 at 21:05
1

I found this in the Conditional Tags codex, section 2.12:

"Note: There is no function to check if a page is a sub-page. We can get around the problem:

if ( is_page() && $post->post_parent > 0 ) { 
   echo "This is a child page";
}

So I've altered it as below and will just use the wp_list_pages function's "title_li" attribute to display "select a sub-page" (not exactly what I was looking for but simple enough and I can work with it for the design of the website I need it for).

<h1><?php echo get_the_title($post->post_parent);?></h1>
<h2><?php if (is_page() && $post->post_parent > 0 ) { the_title();}?></h2>

http://codex.wordpress.org/Conditional_Tags#A_PAGE_Page

0
<?php if ( $post->post_parent ): ?>
<h1><?php echo get_the_title($post->post_parent); ?></h1>
<h2><?php the_title(); ?></h2>
<?php else: ?>
<h1><?php the_title(); ?></h1>
<?php if ( get_pages( array('child_of' => $post->ID) ) ): ?>
<h2>select a sub-page</h2>
<?php endif; ?>
<?php endif; ?>

Something like this should do the job. Not tested, sa few bugs/typos may sit in there.

  • I would use if ( $pages = wp_list_pages( array( 'child_of' => $post->ID, 'echo' => false ) ) ) and then just echo $pages - less hits on the database :) – TheDeadMedic Apr 17 '14 at 8:55
  • @TheDeadMedic Yes, but there is no guarantee that $pages will be empty string or false if no pages exists. There is also chance that codeview would like to replace wp_list_pages with wp_dropdown_pages or something else... – Krzysiek Dróżdż Apr 17 '14 at 11:14
  • But there is. If there aren't any pages, $pages will indeed be empty. And you can use the same arguments with wp_dropdown_pages too. Anyway, just what I would do, you're not wrong either. – TheDeadMedic Apr 17 '14 at 11:56
  • answer is not working, syntax error, unexpected '=>' on line 6. I'm not sure how to fix/change. – codeview Apr 17 '14 at 15:49
  • @TheDeadMedic - can you provide a full answer, as that sounds like a good solution too but I don't know how to execute what you've briefly described. – codeview Apr 17 '14 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.