Support » Fixing WordPress » displaying parent & child pages

  • I have this code that will let me display child-pages on parent-page with featured image but the thing is i want it to display it on the child-pages as well and also to include the parent page in the same list.
    I also want to make sure if there are no child pages the code won’t execute anything.

    I have been stuck on this for some time now any help would be great = )

    Thanks

    <?php
    $child_pages = $wpdb->get_results("SELECT *    FROM $wpdb->posts WHERE post_parent = ".$post->ID."    AND post_type = 'page' ORDER BY menu_order", 'OBJECT');    ?>
    <?php if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); ?>
    
    <div class="child-thumb">
      <?php echo get_the_post_thumbnail($pageChild->ID, 'thumbnail'); ?>
     <a href="<?php echo  get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo $pageChild->post_title; ?></a>
    </div>
    <?php endforeach; endif;
    ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • Here is some code I’ve used to do something similar… counting child pages and ancestors and displaying different page lists depending on the results. I don’t have time to weed it down to something more general right now, but let me know if you need help with it, and I can try to pair it down for your needs.

    <?php	$children = get_pages('child_of='.$post->ID);
    		if( count( $children ) != 0 ) {
    			$ancestors = get_post_ancestors($post);
    			if (count($ancestors) >= 3) {
    			    $parent = $ancestors[0];
    				$pTitle = get_the_title($ancestors[0]);
    			    wp_list_pages('exclude=94,97,99&title_li=' . $pTitle . '&depth=0&child_of=' . $parent);
    			} if (count($ancestors) == 2) {
    			    $parent = $ancestors[0];
    				$pTitle = get_the_title($ancestors[0]);
    			    wp_list_pages('exclude=94,97,99&title_li=' . $pTitle . '&depth=2&child_of=' . $parent);
    			} if (count($ancestors) == 1) {
    			    $parent = $ancestors[0];
    				$pTitle = get_the_title($ancestors[0]);
    			    wp_list_pages('exclude=94,97,99&title_li=' . $pTitle . '&depth=2&child_of=' . $parent);
    			} if (count($ancestors) == 0) {
    			    $parent = $ancestors[0];
    				$pTitle = get_the_title($ancestors[0]);
    			    wp_list_pages('exclude=94,97,99&title_li=' . $pTitle . '&depth=1&child_of=' .$post->ID);
    			}
    
    			 } else {
    			$ancestors = get_post_ancestors($post);
    			if (count($ancestors) >= 3) {
    			    $parent = $ancestors[1];
    				$pTitle = get_the_title($ancestors[1]);
    			    wp_list_pages('exclude=94,97,99&title_li=' . $pTitle . '&depth=2&child_of=' . $parent);
    			} if (count($ancestors) == 2) {
    			    $parent = $ancestors[1];
    				$pTitle = get_the_title($ancestors[1]);
    			    wp_list_pages('exclude=94,97,99&title_li=' . $pTitle . '&depth=2&child_of=' . $parent);
    			}  if (count($ancestors) == 1) {
    			    $parent = $ancestors[0];
    				$pTitle = get_the_title($ancestors[0]);
    			    wp_list_pages('exclude=94,97,99&title_li=' . $pTitle . '&depth=1&child_of=' . $parent);
    			}
    		 }
    ?>

    Hi thanks for the help but how can i put link to the parent page as well?

    This code includes the parent

    <ul>
    <?php	$children = get_pages('child_of='.$post->ID);
    if( count( $children ) != 0 ) {
    $ancestors = get_post_ancestors($post);
    if (count($ancestors) >= 3) {
    $parent = $ancestors[0]; // third level page ID
    $pTitle = get_the_title($ancestors[0]);
    $pLink = get_permalink($ancestors[0]);
    
    ?> <ul><li><a href="<?php echo $pLink ?>"><?php echo $pTitle ?></a><ul><?php
    wp_list_pages('exclude=94,97,99&title_li=&depth=2&child_of=' . $parent);
    ?> </ul></li></ul><?php
    
    } if (count($ancestors) == 2) {
    $parent = $ancestors[0]; // third level page ID
    $pTitle = get_the_title($ancestors[0]);
    $pLink = get_permalink($ancestors[0]);
    ?> <ul><li><a href="<?php echo $pLink ?>"><?php echo $pTitle ?></a><ul><?php
    wp_list_pages('exclude=94,97,99&title_li=&depth=2&child_of=' . $parent);
    ?> </ul></li></ul><?php
    } if (count($ancestors) == 1) {
    $parent = $ancestors[0]; // third level page ID
    $pTitle = get_the_title($ancestors[0]);
    $pLink = get_permalink($ancestors[0]);
    ?> <ul><li><a href="<?php echo $pLink ?>"><?php echo $pTitle ?></a><ul><?php
    wp_list_pages('exclude=94,97,99&title_li=&depth=2&child_of=' . $parent);
    ?> </ul></li></ul><?php
    } if (count($ancestors) == 0) {
    $parent = $ancestors[0]; // third level page ID
    $pTitle = get_the_title($ancestors[0]);
    $pLink = get_permalink($ancestors[0]);
    ?> <ul><li><a href="<?php echo $pLink ?>"><?php echo $pTitle ?></a><ul><?php
    wp_list_pages('exclude=94,97,99&title_li=&depth=1&child_of=' .$post->ID);
    ?> </ul></li></ul><?php
    }
    
    } else {
    $ancestors = get_post_ancestors($post);
    if (count($ancestors) >= 3) {
    $parent = $ancestors[1]; // third level page ID
    $pTitle = get_the_title($ancestors[1]);
    $pLink = get_permalink($ancestors[1]);
    ?> <ul><li><a href="<?php echo $pLink ?>"><?php echo $pTitle ?></a><ul><?php
    wp_list_pages('exclude=94,97,99&title_li=&depth=2&child_of=' . $parent);
    ?> </ul></li></ul><?php
    } if (count($ancestors) == 2) {
    $parent = $ancestors[1]; // third level page ID
    $pTitle = get_the_title($ancestors[1]);
    $pLink = get_permalink($ancestors[1]);
    ?> <ul><li><a href="<?php echo $pLink ?>"><?php echo $pTitle ?></a><ul><?php
    wp_list_pages('exclude=94,97,99&title_li=&depth=2&child_of=' . $parent);
    ?> </ul></li></ul><?php
    }  if (count($ancestors) == 1) {
    $parent = $ancestors[0]; // third level page ID
    $pTitle = get_the_title($ancestors[0]);
    $pLink = get_permalink($ancestors[0]);
    ?> <ul><li><a href="<?php echo $pLink ?>"><?php echo $pTitle ?></a><ul><?php
    wp_list_pages('exclude=94,97,99&title_li=&depth=1&child_of=' . $parent);
    ?> </ul></li></ul><?php
    }
    }
    ?>
    </ul>
    esmi

    (@esmi)

    Forum Moderator

    Add to functions.php

    function my_page_tree($this_page) {
    	$pagelist = '';
    	if( !$this_page->post_parent ) {
    		$children = wp_list_pages('title_li=&child_of='.$this_page->ID.'&echo=0');
    		if( $children ) {
    			$pagelist .= '<li class="current_page_item"><a href="'.  get_page_link($this_page->ID) .'">' . $this_page->post_title . '</a>';
    			$pagelist .= '<ul>' . $children . '</ul>';
    			$pagelist .= '</li>';
    		}
    	}
    	elseif( $this_page->ancestors ) {
    		// get the top ID of this page. Page ids DESC so top level ID is the last one
    		$ancestor = end( get_post_ancestors($this_page) );
    		$pagelist .= wp_list_pages('title_li=&include='.$ancestor.'&echo=0');
    		$pagelist = str_replace('</li>', '', $pagelist);
    		$pagelist .= '<ul>' . wp_list_pages('title_li=&child_of='.$ancestor.'&echo=0') .'</ul></li>';
    	}
    	return $pagelist;
    }

    Call in the relevant template using:

    <?php echo my_page_tree($post);?>

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘displaying parent & child pages’ is closed to new replies.