Support » How-To and Troubleshooting » Show a certain number of child pages for a parent on the homepage

Show a certain number of child pages for a parent on the homepage

  • Hi all,

    I’m about a 1 out of 100 for my WordPress skill level but I’m giving it a go and doing well so far.

    Here’s my dilemma.

    I’ve got the current page structure

    – Portfolio
    —– Portfolio item 1
    —– Portfolio item 2
    —– Portfolio item 3
    —– Portfolio item 4
    —– Portfolio item 5
    —– Portfolio item 6
    —– etc

    I can get the portfolio page to show each of the portfolio item pages feature image and title fine.

    My next step it so the latest 4 portfolio items feature image and title on the homepage.

    Currently the code I’m using on the portfolio page to show the child pages is:

    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

    $children = get_pages(‘child_of=’.$post->ID);
    if( count( $children ) != 0 ) { }
    else { echo “There are currently no portfolio items to show”; }


    $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 ); ?>

    <li class=”col2″>
    ID); ?>” rel=”bookmark” title=”<?php echo $pageChild->post_title; ?>”><?php echo get_the_post_thumbnail($pageChild->ID, ‘medium’); ?>
    <div class=”portfolio-thumbnail-info”>
    <h3 class=”title”>ID); ?>” rel=”bookmark” title=”<?php echo $pageChild->post_title; ?>”><?php echo $pageChild->post_title; ?></h3>

    <?php endforeach; endif;

    <?php endwhile; // end of the loop. ?>

    All I need now is the code to include on the homepage template to show the latest 4 portfolio item pages.

    I must stress, I’m a complete beginner with WordPress and PHP. The code I used above was found on a forum which I tweaed to include the title and feature image.

    Currently using WordPress 3.3


Viewing 1 replies (of 1 total)
  • I’ve managed to get it to work using:

    	$mypages = get_pages( array( 'child_of' => 6, 'sort_column' => 'post_date', 'sort_order' => 'desc' ) );
    	foreach( $mypages as $page ) {
    		$content = $page->post_content;
    		$content = apply_filters( 'the_content', $content );
    	    if ( $count < 4 ) {  // only process 10 ?>
    			<div class="one-third home-entry">
    			 	<a href="<?php echo  get_permalink($page->ID); ?>" class="portfolio-image-hover" rel="bookmark" title="<?php echo $page->post_title; ?>"><?php echo get_the_post_thumbnail($page->ID, 'home-page-portfolio'); ?></a> <br />
    			 	<a href="<?php echo  get_permalink($page->ID); ?>" rel="bookmark" title="<?php echo $page->post_title; ?>"><h2 class="title"><?php echo $page->post_title; ?></h2></a>
Viewing 1 replies (of 1 total)
  • The topic ‘Show a certain number of child pages for a parent on the homepage’ is closed to new replies.