WordPress.org

Support

Support » How-To and Troubleshooting » Displaying child pages

Displaying child pages

  • I have some code that displays the headings and post thumbnails of a pages’ children and grand children.

    I’d like to edit it so that the first child is a h2, and then wrap a div around each next child.

    Is this possible?

    <?php
    	$mypages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');
    
    	foreach($mypages as $page)
    	{
    		$content = $page->post_content;
    		if(!$content) // Check for empty page
    			continue;
    
    		$content = apply_filters('the_content', $content);
    	?>
    		<h2><a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a></h2>
    		<?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?>
    	<?php
    	}
    ?>
Viewing 9 replies - 1 through 9 (of 9 total)
  • Didn’t test but you could use something like this:

    before your foreach put

    $counter=0;

    then in your foreach loop

    if $page->post_parent == $post->ID {  // is this page a child
      $counter++;
      if $counter == 1 {
        //do your h2 thing here
      } else {
        // do your div thing here
      }
    }

    Thank Michael!

    I’m having some trouble with that code, I’ve edited what I’ve got and come up with this:

    <?php
    	$mypages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');
    	$counter=0;
    	foreach($mypages as $page)
    	{
    	if $page->post_parent == $post->ID {  // is this page a child
    	$counter++;
    	if $counter == 1 {
    	?>
    	<!--h2 thing-->
    	<?
    	} else {
    	?>
    	<!--div thing-->
    	<?
    	}
    	}
    ?>

    But this is giving me a blank page, any ideas?

    Oops, well at the very least

    if $page->post_parent == $post->ID {  // is this page a child

    should be

    if ($page->post_parent == $post->ID) {  // is this page a child

    Still nothing I’m afraid!

    <?php
    	$mypages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');
    	$counter=0;
    	foreach($mypages as $page)
    	{
    	if ($page->post_parent == $post->ID) {  // is this page a child
    	$counter++;
    	if $counter == 1 {
    	?>
    	<!--h2 thing-->
    	<?
    	} else {
    	?>
    	<!--div thing-->
    	<?
    	}
    	}
    ?>

    Jeesh another one

    if $counter == 1 {

    should be

    if ($counter == 1) {

    Thanks Michael, it’s now showing HTML, there was a missing } at the end.

    Here’s the code I have now:

    <?php
    	$mypages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');
    	$counter=0;
    	foreach($mypages as $page)
    	{
    		if ($page->post_parent == $post->ID) {  // is this page a child
    		$counter++;
    		if ($counter == 1) {
    
    			echo '<h2>'.get_the_title($post->ID).'</h2>';
    		}
    		else {
    			?>
    		<div class="quarterWidth boxShadow">
    			<h2><a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a></h2>
    			<a href="<?php echo get_page_link($page->ID) ?>"><?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?></a>
    			<p class="button button180"><a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a></p>
    		</div>
    			<?php
    			}
    		}
    	}
    ?>

    This is only showing the parent and first child, in that order. I need it go one level further, so that the first child is the h2, and the grand-child is in the div, is that possible?

    Then wouldn’t you’d need to put the div section outside the if ($page->post_parent == $post->ID) { construct?

    Apparently so!

    Sorry, I wouldn’t have known how to do that, it works perfectly now, thanks so much you’ve been very patient and helpful, thanks.

    One other thing, could this code be edited to work at a higher level on a page “up” in the hierarchy?

    For example, If I click the top level page, or “category name” in this case, it would then show all the first child as a h2, and the grand children as the div mentioned above?

    This code you provided works perfectly to show the first child category name, and the products (grand children) under that page.

    Hope this makes sense.

    One other thing, could this code be edited to work at a higher level on a page “up” in the hierarchy?

    I’d say yes, because regardless of hierachy depth, you are testing $page->post_parent.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Displaying child pages’ is closed to new replies.
Skip to toolbar