0

In my main loop, I need to incorporate proper html markup into a conditional to test if title of parent page matches title of child page and do (or do not) display certain content. Default might be to keep h1 tag , otherwise do not display.

<!-- basic loop -->
<?php if (have_posts()) : while (have_posts()) : the_post() ; ?>
<h2> 
    <?php $parent_title = get_the_title($post->post_parent); echo $parent_title; ?>
</h2>

<?php
$children = get_pages('child_of='.$post->ID);
if( count( $children ) != 0 ) { 

echo 'NO children';

}
else { echo 'has children';}
?>


    <h1 style="margin-bottom:12px"><?php the_title(); ?></h1> 
    <?php the_content(); ?>
<?php endwhile; endif; ?> 

ex) I want h1 to show here... http://howlingwolfmedia.com/site3/my-club/facilities/

but not here: http://howlingwolfmedia.com/site3/my-club/

solution here as function:

function testchildren () { 
   global $post; 
    $childtest = get_pages('child_of='.$post->ID); 
       if( count( $childtest ) != 0 ) { 
       return false; 
       } else { 
       return true; 
      } 
} 

and implementation as markup:

   <?php if (have_posts()) : while (have_posts()) : the_post() ; ?> 
       <h2><?php $parent_title = get_the_title($post->post_parent); echo $parent_title; ?> </h2>
    <?php if ( testchildren() ) : ?> 
        <h1 style="margin-top:6px; margin-bottom:12px;"><?php the_title(); ?></h1>
    <?php endif; ?>
    <?php the_content(); ?>
    <?php endwhile; endif; ?>
7
  • 2
    What is the question exactly? You have the parent title. Just compare it to the child.
    – s_ha_dum
    Commented Jan 22, 2014 at 2:46
  • I created a function called testchildren... ` function testchildren () { global $post; $childtest = get_pages('child_of='.$post->ID); if( count( $childtest ) != 0 ) { return false; } else { return true; } }` and html markup: <?php if (have_posts()) : while (have_posts()) : the_post() ; ?> <h2><?php $parent_title = get_the_title($post->post_parent); echo $parent_title; ?> </h2> <?php if ( testchildren() ) : ?> <h1 style="margin-top:6px; margin-bottom:12px;"><?php the_title(); ?></h1> <?php endif; ?> <?php the_content(); ?> <?php endwhile; endif; ?>
    – lberelson
    Commented Jan 28, 2014 at 17:14
  • Please edit that code into the question.
    – s_ha_dum
    Commented Jan 28, 2014 at 17:47
  • I added backticks per instructions and that's how it formatted. See function here: pastebin.com/PBJjtXuG and implementation here: pastebin.com/98MJCWLg
    – lberelson
    Commented Jan 29, 2014 at 20:27
  • 1
    I didn't ask you to format the comment. I asked you to edit the comment into the question. Code formatting in comments is very minimal.
    – s_ha_dum
    Commented Jan 29, 2014 at 22:37

1 Answer 1

0

solution here as function:

function testchildren () { 
   global $post; 
    $childtest = get_pages('child_of='.$post->ID); 
       if( count( $childtest ) != 0 ) { 
       return false; 
       } else { 
       return true; 
      } 
} 

and implementation as markup:

   <?php if (have_posts()) : while (have_posts()) : the_post() ; ?> 
       <h2><?php $parent_title = get_the_title($post->post_parent); echo $parent_title; ?> </h2>
    <?php if ( testchildren() ) : ?> 
        <h1 style="margin-top:6px; margin-bottom:12px;"><?php the_title(); ?></h1>
    <?php endif; ?>
    <?php the_content(); ?>
    <?php endwhile; endif; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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