1

On my parent page, i have a custom page template calling another template:

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

<div class="wrapper">


<?php get_template_part( 'template', 'page-section' ); ?>

<?php endwhile; endif; wp_reset_postdata(); ?>

Inside template-page-section.php, I have the following:

<?php
/*
Template Name: Page Section
*/
$args = array(
    'post_parent' => 9,
    'post_type' => 'page',
    'orderby' => 'menu_order',
    'posts_per_page' => -1,
    'order' => 'ASC'
);

$wpq = new WP_Query( $args ); ?>

<?php while ( $wpq->have_posts() ) : $wpq->the_post(); ?>

<?php if ( $post->ID == 101 ) {
    include( 'template-slider.php' );
} ?> 

<div class="page-section">
    <h1><?php the_title(); ?></h1>
    <?php /* The loop */ ?>
        <?php the_content(); ?>
</div>

<?php endwhile; wp_reset_postdata();?>

Within the template-slider.php, is the following:

<div id="slider-container">
    <ul id="slider">
        <? $query = get_pages( 
            array(
                'post_type' => 'slides',
                'orderby' => 'menu_order',
                'posts_per_page' => -1
            ));


            foreach( $query as $post ) { 
            setup_postdata( $post ); ?>
            <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail', $thumbsize[0] ); ?>

            <li>
            <img src="<?php echo $image[0]; ?>">
            </li>
            <?php } wp_reset_postdata(); ?>

        </ul>   
    </div>      

The issue is that once wordpress hits the loop within the slider-template, the information that it echos for the post content is not a child post of the parent, but the parents content.

Can anyone tell me what I'm doing wrong? I cant figure it out!

1
  • In the template-slider.php file, you do not need to include the setup_postdata( $post ); line as get_pages() will include the post ID in the array. Also, you may want to rename $post as it is a global WP uses; not sure if it's any issue here, though. Commented Oct 24, 2013 at 22:20

1 Answer 1

1

This is just an untested guess, but try the following:

template-page-section.php

$wpq = get_posts( $args );
if( $wpq ) {
    foreach( $wpq as $p )
    {
        if ( $p->ID == 101 ) {
            include( 'template-slider.php' );
        }
        ?>
            <div class="page-section">
                <h1><?php echo $p->post_title; ?></h1>
                <?php echo $p->post_content; ?>
            </div>
        <?php
    }
}

template-slider.php

<div id="slider-container">
    <ul id="slider">
        <?php 
        $query_pages = get_pages( 
            array(
                'post_type' => 'slides',
                'orderby' => 'menu_order',
                'posts_per_page' => -1
        ));
        if( $query_pages )
        {
            foreach( $query_pages as $pg ) { 
                $image = wp_get_attachment_image_src( get_post_thumbnail_id( $pg->ID ), 'single-post-thumbnail', $thumbsize[0] ); ?>
                <li>
                <img src="<?php echo $image[0]; ?>">
                </li><?php 
            } 
        } ?>
    </ul>   
</div>

Reference: When should you use WP_Query vs query_posts() vs get_posts()?

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks so much @brasofilo! Also I appreciate the link to explain your answer!
Well, simply dumping a correct code doesn't make a good answer, really glad it worked at prime-time :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.