多个自定义帖子查询无效

时间:2018-06-03 20:20:38

标签: wordpress post

我试图在同一页面中多次显示相同的自定义帖子查询。帖子之间是基于一种自定义帖子类型调用不同的自定义分类。当我试图第二次调用相同的自定义帖子时它不起作用。但showposts = 2正在运行,但我需要为一个帖子查询显示一个帖子。这是代码:

        <div class="col-sm-6">
            <?php

                $querevent = new WP_Query( array(
                'post_type' => 'tatstory',          // name of post type.
                'showposts' => 1,
            ) );

            if ( $querevent->have_posts() ):
                  // Yep, we have posts, so let's loop through them.
                  while ( $querevent->have_posts() ) : $querevent->the_post();  ?>

                <?php if ( has_term('event','eventname' ) && has_term('featured-in-story-page','fetstory' )) { ?>
                        <div class="listing_inner">
                            <div class="listing_img">
                                <?php the_post_thumbnail( 'shop-story', array( 'class' => 'img-fluid' ) ); ?>
                            </div>
                            <div class="listing_texts">
                                <p class="event yellow">Event</p>
                                <h2><?php the_title(); ?></h2>
                            </div>
                        </div>
                <?php                                                                                       
                }  ?>


            <?php endwhile;

        else :
          // No, we don't have any posts, so maybe we display a nice message
          echo "<p class='no-posts'>" . __( "Sorry, there are no posts at this time." ) . "</p>";
        endif; 
            ?>

        </div>


        <div class="col-sm-6">
             <?php

                $quernews = new WP_Query( array(
                'post_type' => 'tatstory',          // name of post type.
                'showposts' => 1,
            ) );

            if ( $quernews->have_posts() ):
                  // Yep, we have posts, so let's loop through them.
                  while ( $quernews->have_posts() ) : $quernews->the_post();  ?>
               <?php if ( has_term('news','eventname' ) && has_term('featured-in-story-page','fetstory' )) { ?>   

            <div class="listing_inner">
                <div class="listing_img">
                    <?php the_post_thumbnail( 'shop-newscat', array( 'class' => 'img-fluid' ) ); ?>
                </div>
                <div class="listing_texts_right">
                    <p class="event blue">News</p>
                    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                </div>
            </div>
             <?php }  ?>
            <?php endwhile;
            wp_reset_postdata(); 
        else :
          // No, we don't have any posts, so maybe we display a nice message
          echo "<p class='no-posts'>" . __( "Sorry, there are no posts at this time." ) . "</p>";
        endif; 

            ?>

1 个答案:

答案 0 :(得分:0)

首先我会改变你的WP_Query,例如:

$query = new WP_Query( array(
    'post_type' => 'tatstory',
    'posts_per_page' => '1',
    'tax_query' => array(
        array (
            'taxonomy' => 'eventname',
            'field' => 'slug',
            'terms' => 'news',
        ),
        array (
            'taxonomy' => 'fetstory',
            'field' => 'slug',
            'terms' => 'featured-in-story-page',
        )
    ),
) );
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        // display content
    }
} else {
    // display when no posts found
}

wp_reset_postdata();     // Restore original Post Data

查询只会收集设置正确字词的帖子。

在末尾(wp_reset_postdata())恢复原始发布数据可能会帮助您再次调用新的WP_Query。但我不知道为什么你不能在单页模板中调用它两次。