• Resolved kdn

    (@kdn)


    Hello.

    It would be great if someone have input on how I could solve the following problem. I have searched the forum for a while without finding an answer to my problem. I want to output the content of the subpages on the parent page.

    An illustration

    The code I’m using is the following:

    <section class="grid_8">
    	<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    	<?php
    		$pageID = $wp_query->post->ID;
    	?>
    	<article>
     		<h2><?php the_title(); ?></h2>
        	<?php the_content(); ?>
     	</article> <!-- close the article -->
    	<?php endwhile; else: ?>
     		<p>Sorry, no posts matched your criteria.</p>
     	<?php endif; ?>
    </section>
    <?php
    	//the missing link....
    ?>
    
    <?php if($subpageID>>'0'):?>
    <aside class="grid_4">
    
    	<?php $child = new WP_Query('page_id=' . $subpageID); while ($child->have_posts()) : $child->the_post(); $do_not_duplicate = $post->ID; ?>
    	<h2><?php the_title(); ?></h2>
        <?php the_content(); ?>
    	<?php endwhile; ?>
    </aside>

    The thing I need help with is “the missing link”. How to get all the subpage ID’s from the original loop?

    Is there a better way to do this?

Viewing 5 replies - 1 through 5 (of 5 total)
  • How about using this inside the first Loop:

    <?php
    $args = array(
    	'numberposts' => -1,
    	'post_parent' => $post->ID,
    	'post_type' => 'page',
    	'post_status' => 'publish',
    	'orderby' => 'menu_order,title',
    	'order' => 'ASC'
    );
    $my_pagelist = &get_children($args);
    ?>

    Thread Starter kdn

    (@kdn)

    Thanks, that was the bit I were missing.

    Thread Starter kdn

    (@kdn)

    Sorry for the noob questions here but Im not a PHP guru.

    I get the correct array output from esmi’s code above but how do I access it?

    The array looks like this

    Array (
    [55] => stdClass Object (
    [ID] => 55
    [post_author] => 2
    [post_date] => 2009-11-03 21:43:33
    [post_date_gmt] => 2009-11-03 20:43:33
    [post_content] => Postcontent.
    [post_title] => subpage1
    [post_excerpt] =>
    [post_status] => publish
    [comment_status] => open
    [ping_status] => open
    [post_password] =>
    [post_name] => pris
    [to_ping] =>
    [pinged] =>
    [post_modified] => 2009-11-03 21:43:33
    [post_modified_gmt] => 2009-11-03 20:43:33
    [post_content_filtered] =>
    [post_parent] => 30
    [guid] => https://custome.url/page/
    [menu_order] => 0
    [post_type] => page
    [post_mime_type] =>
    [comment_count] => 0 
    
    )
    [59] => stdClass Object (
    [ID] => 59 [post_author] => 2
    [post_date] => 2009-11-04 02:51:38
    [post_date_gmt] => 2009-11-04 01:51:38
    [post_content] => postcontent.
    [post_title] => subpage2
    [post_excerpt] =>
    [post_status] => publish
    [comment_status] => open
    [ping_status] => open
    [post_password] =>
    [post_name] => subpage2
    [to_ping] =>
    [pinged] =>
    [post_modified] => 2009-11-04 02:51:38
    [post_modified_gmt] => 2009-11-04 01:51:38
    [post_content_filtered] =>
    [post_parent] => 30
    [guid] => https://custome.url/page/
    [menu_order] => 0
    [post_type] => page
    [post_mime_type] =>
    [comment_count] => 0 )
    
    )

    The initial array name change for each subpage.

    How do I parse out only the ID data? And what if there is multiple entries?

    Try something like:

    <?php
    if ($my_pagelist) :
    foreach($my_pagelist as $my_child) :
    $my_child_slug = $my_slug . '/'. $my_child->post_name.'/';
    ?>
    <h3 class="post-title"><a href="<?php echo $my_child_slug;?>"><?php echo $my_child->post_title;?></a></h3>
    <?php
    $my_content = apply_filters('the_content',$my_child->post_content);
    $my_content = str_replace(']]>', ']]>', $my_content);
    echo $my_content;
    ?>

    That works for me when I’ve been working on something very similar.

    Thread Starter kdn

    (@kdn)

    Thanks a lot again.

    I did it with a simple foreach loop since the project only require output from one subpage.

    $i = 0;
    foreach ($my_subpagelist as $key => $value) {
    		$i++; 
    
    		if ( $i == 1 )
    		break;
    		}
    		$subpageID = $key;

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Display subpage content on parent page’ is closed to new replies.