Forums

[resolved] Displaying children content on parent page (10 posts)

  1. Hey folks,
    I'm trying to set up a template with 4 content areas.

    In the admin, I've set this up as a page, with three children.

    I'm using the following code, but I think that there's something wrong with my loop:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    	<?php the_content(); //display the parent content ?>
    	<hr />
    	<?php $thispage=$post->ID; ?>
    	 <?php $childpages = query_posts('showposts=3orderby=menu_order&order=asc&post_type=page&post_parent='.$thispage);
    	if($childpages){ // display the children content
    	      	$i=1;
    	       	foreach($childpages as $post) :
    	         setup_postdata($post);         ?>
    	       <h2><?php the_title(); ?></h2>
    		<?php the_content();?>
                  <hr />
    	<?php
    	    $i++;
    	    endforeach;
    	 } ?>
    	<?php endwhile; endif; ?>

    When I use the above code, I get the parent content followed by the hr, then the children also with hr, but then an extra content piece.

    Here's what the page looks like right now - http://www.ralphsultanmla.ca/wp/ralphs-newsletter-for-july-2010/

    Anyone know how I can fix this loop, so that the last piece of content doesn't appear?

  2. mitten
    Member
    Posted 2 years ago #

    Maybe it's related to this: http://wordpress.org/support/topic/395373

    In that topic, it suggests using posts_per_page instead of showposts.

    Also weird that the fourth isn't displaying an <h2> heading like the others.

  3. CatherineWinters
    Member
    Posted 2 years ago #

    You're missing an ampersand between 'showposts=3' and 'orderby=menu_order', so it's not recognizing those arguments.

    Also, I don't think you're doing anything with the variable $i. You're incrementing it, but then not checking it against anything as you would in a standard FOR loop, so you can remove the lines '$i=1;' and '$i++;'

  4. mitten
    Member
    Posted 2 years ago #

    You could also totally kludge it and instead of doing foreach with the auto increment, you could do for i < 4 and force it to stop after 3 that way.

  5. CatherineWinters
    Member
    Posted 2 years ago #

    Mitten's right; if the query's fixed, it'll definitely only return three posts, but for cases where you can't rely upon the input being clean, limiting the number of runs through the loop is often necessary.

    Plus, then you'd get some good use out of the $i variable! :)

  6. Michael Fields
    Theme Wrangler
    Posted 2 years ago #

    crondeau,
    It looks like you have nested your loops which is really not necessary. I would try something like the following:

    <?php 
    
    /* Query and display the parent. */
    if ( have_posts() ) {
    	while ( have_posts() ) {
    		the_post();
    		the_content();
    		print "\n\t" . '<hr />';
    		$thispage=$post->ID;
    	}
    }
    
    /* Query for children. */
    $children = get_pages( array(
    	'post_parent' => $thispage,
    	'numberposts' => 3,
    	'orderby' => 'menu_order',
    	'order' => 'ASC',
    	));
    
    /* Back up global $post object. */
    $post_backup = $post;
    
    /* Display children pages. */
    foreach( (array) $children as $post ) {
    	setup_postdata( $post );
    	the_title( '<h2>', '</h2>' );
    	the_content();
    	print "\n\t" . '<hr />';
    }
    
    ?>
  7. Michael Fields
    Theme Wrangler
    Posted 2 years ago #

    Opps, forget this part which should go at the end of the above code:

    /* Restore global $post object. */
    $post = $post_backup;
  8. Thanks a lot for your answers. I'll try it out and let you know how it goes.

  9. I ended up using this code and I can now display the parent and the three children underneath all on one page.

    Thanks for your help.

    <?php if ( have_posts() ) {  /* Query and display the parent. */
    	while ( have_posts() ) {
    	the_post();
    	the_content();
    	$thispage=$post->ID;
    	}
    } ?>
    
    <?php $childpages = query_posts('post_per_page=3&orderby=menu_order&order=asc&post_type=page&post_parent='.$thispage);
    	if($childpages){ /* display the children content  */
    	       	foreach ($childpages as $post) :
    	        setup_postdata($post); ?>
    	        <h2><?php the_title(); ?></h2>
    		<?php the_content();?>
                  <hr />
    	 <?php
    	  endforeach;
    	 } ?>
  10. Michael Fields
    Theme Wrangler
    Posted 2 years ago #

    crondeau,
    Glad you got it all figured out. Please note that that when you are accessing the global $post variable (as you are in the loop for $childpages) that you back it up before hand and then reassign it after you are done using it. Not doing so can have undesired effects throughout your theme.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags