简体   繁体   English

自定义WP-Query,显示“内容”页面中的所有帖子

[英]Custom WP-Query showing all posts in “content” page

I have a custom post type "event" with a date as custom post_meta . 我有一个自定义帖子类型“事件”,其日期为自定义post_meta Using the template's built-in loop would be fine but I needed to sort by date, I created a custom query: 使用模板的内置循环会很好,但是我需要按日期排序,因此创建了一个自定义查询:

<?php // the query
    $args = array(
        'post_type'         => 'event' ,
        'orderby'           => 'meta_value',
        'meta_query'        => array(
            array(
                'key'       => 'event_start_date',
                'type'      => 'DATE'
            )
        ),
        'posts_per_page'    =>  20, 
        'order'             =>  'DESC'
        );

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

        <!-- the loop -->
        <?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>

            <?php get_template_part( 'loop-templates/content', 'event' ); ?>

            <?php the_post_navigation( array(
                'prev_text'                  => __( '<span class="nav-prev">Prev article: %title</span>' ),
                'next_text'                  => __( '<span class="nav-next">Next article: %title</span>' ),
                'in_same_term'               => true,
                'taxonomy'                   => __( 'category' ),
                'screen_reader_text' => __( 'Continue Reading' ),
            ) ); ?>

            <?php
            // If comments are open or we have at least one comment, load up the comment template
            if ( comments_open() || get_comments_number() ) :
                comments_template();
            endif;
            ?>

        <?php endwhile; // end of the loop. ?>

The problem I have is that while the "archive" page is now sorting by date, as expected, when I go to the single page by clicking one of the post's permalink, it shows the wrong post content and then other posts below it. 我遇到的问题是,“存档”页面现在按预期按日期排序,当我单击某个帖子的永久链接转到单个页面时,它显示错误的帖子内容,然后显示其下的其他帖子。 It's as if it's executing the query in the content template. 就像在内容模板中执行查询一样。

Does anyone have any ideas? 有人有什么想法吗?

You can use different query arguments for pages. 您可以对页面使用不同的查询参数。 Try to use conditional tags https://codex.wordpress.org/Conditional_Tags#Single_Post 尝试使用条件标签https://codex.wordpress.org/Conditional_Tags#Single_Post

I think you need to call wp_reset_postdata() after your custom query to reset various global post variables. 我认为您需要在自定义查询后调用wp_reset_postdata()来重置各种全局发布变量。

In your case, you'd add <?php wp_reset_postdata(); ?> 对于您的情况,您可以添加<?php wp_reset_postdata(); ?> <?php wp_reset_postdata(); ?> immediately after your custom loop (ie after the endwhile statement). <?php wp_reset_postdata(); ?>在您的自定义循环之后(即在endwhile语句之后)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM