WordPress.org

Ready to get started?Download WordPress

Forums

Displaying child pages (10 posts)

  1. tjobbe
    Member
    Posted 3 years ago #

    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
    	}
    ?>
  2. MichaelH
    Member
    Posted 3 years ago #

    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
      }
    }
  3. tjobbe
    Member
    Posted 3 years ago #

    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?

  4. MichaelH
    Member
    Posted 3 years ago #

    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
  5. tjobbe
    Member
    Posted 3 years ago #

    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-->
    	<?
    	}
    	}
    ?>
  6. MichaelH
    Member
    Posted 3 years ago #

    Jeesh another one

    if $counter == 1 {

    should be

    if ($counter == 1) {
  7. tjobbe
    Member
    Posted 3 years ago #

    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?

  8. MichaelH
    Member
    Posted 3 years ago #

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

  9. tjobbe
    Member
    Posted 3 years ago #

    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.

  10. MichaelH
    Member
    Posted 3 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.