循环错误中的Wordpress循环

时间:2013-10-24 21:36:58

标签: javascript php jquery wordpress

在我的父页面上,我有一个自定义页面模板,调用另一个模板:

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

<div class="wrapper">


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

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

在template-page-section.php中,我有以下内容:

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

在template-slider.php中,如下:

<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>      

问题是,一旦wordpress点击了滑块模板中的循环,它回发帖子内容的信息不是父亲的子帖,而是父母的内容。

谁能告诉我我做错了什么?我弄清楚了!

1 个答案:

答案 0 :(得分:1)

这只是一个未经测试的猜测,但请尝试以下方法:

<强>模板的页面-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
    }
}

<强>模板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>

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