如何在WordPress中的同一页面中显示多个查询结果

时间:2015-08-15 06:37:55

标签: wordpress

请查看下面的代码段。我在同一页面中使用多个帖子查询但无法在每个输出中显示输出

<div class="col-md-4 single-content">
<?php       
    $my_query = new WP_Query( 'post_type=news&category_name=current news&posts_per_page=1' );

    if ( $my_query->have_posts() ) : 
        while ( $my_query->have_posts() ) : $my_query->the_post(); ?>

             <h4 class="title"><?php the_title();?></h4>
             <p class="add-info"><?php the_date(); ?></p>

             <?php the_content(); 
        endwhile;
    endif; 
    wp_reset_query();
?>    
</div> <!-- End of single-content -->

<div class="col-md-4 single-content">
    <div class="headbar"><h3>Organization News</h3></div>
        <?php       
            $my_query = new WP_Query( 'post_type=news&category_name=organization news&posts_per_page=1' );

            if ( $my_query->have_posts() ) : 
                while ( $my_query->have_posts() ) : $my_query->the_post();?>

                    <h4 class="title"><?php the_title();?></h4>
                    <p class="add-info"><?php the_date(); ?></p>

                     <?php the_content();
                endwhile;
            endif; 
            wp_reset_query();
        ?>    
</div> <!-- End of single-content -->

它没有在第二个循环中显示php_date函数的结果。有人可以帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

仅在wp_reset_query()https://codex.wordpress.org/Function_Reference/wp_reset_query之后才需要{p> query_posts()。您应该使用wp_reset_postdata()代替。

旁注:正如我的评论中所提到的,您的类别slugs不能包含空格,因此您的查询将无法按预期工作(如果有的话)。

相关问题