分页显示基于日期的archive.php循环的空页

时间:2016-04-08 15:33:44

标签: wordpress loops pagination custom-post-type archive

我使用wordpress并为自定义帖子类型设置了自定义存档页面。 自定义循环获取登录用户注册日期,仅显示在注册时或之后发布的帖子。效果很好。

然而,分页仍然使用主查询,因此如果总共有4个帖子设置为每页2个帖子,则分页总是显示有两个页面,即使由于登录用户注册日期仅显示一个帖子

任何人都可以帮我修改我的内容,以便分页只显示该用户查询超过2个帖子的结果吗?我现在已经尝试了几个小时,使用了我在网上发现的各种变化......

<?php if ( have_posts() ): ?>
        <?php
        # Get the current user's info
        $user_info = get_userdata(get_current_user_id());
        # Use date_parse to cast your date to an array 
        $regdate = date_parse($user_info->user_registered);
        # Set your arguments for WP Query    

        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

        $args = array(
            'post_type'  => 'inner',
            'posts_per_page'         => '2',
            'posts_per_archive_page' => '2',
            'paged'          => $paged,
            'date_query' => array(
                array(
                    'after'    => array(
                        # Setting date to array above allows to call specific values within that date    
                        'year'  => $regdate['year'],
                        'month' => $regdate['month'],
                        'day'   => $regdate['day'],
                    ),
                    # Include posts from the day the user registered  
                    'inclusive' => true,
                ),
            ),
            # Display all posts on a single page.
        );          
        $my_query = new WP_Query( $args );
        while ($my_query->have_posts()) : $my_query->the_post(); 

        get_template_part( 'template-parts/content', get_post_format() );

        endwhile; ?>

        <div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
        <div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>

        <?php else: ?>
            Nada
        <?php endif; ?>

1 个答案:

答案 0 :(得分:2)

使用自定义存档和分页

@Scott Eldo 使用创建自定义查询的方法不会更改自定义存档上的主查询。仅供参考,你是对的,分页仅适用于主查询。

在您的情况下,推荐的方法,我将使用过滤器pre_get_posts来处理自定义存档和分页。请在how to modify main query on post type archive page查看我的答案,您可以使用查询参数找出答案。

但是如果您打算直接在模板中创建查询(即使它不是更改主查询),您需要将自定义查询与在分页中使用的$GLOBALS['wp_query']匹配,并且不要忘记使用{{ 3}}(必须)。看看我的方法与您的代码相关:

<?php if ( have_posts() ): ?>
    <?php
    # Get the current user's info
    $user_info = get_userdata(get_current_user_id());
    # Use date_parse to cast your date to an array 
    $regdate = date_parse($user_info->user_registered);
    # Set your arguments for WP Query    

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

    $args = array(
        'post_type'  => 'inner',
        'posts_per_page'         => '2',
        'posts_per_archive_page' => '2',
        'paged'          => $paged,
        'date_query' => array(
            array(
                'after'    => array(
                    # Setting date to array above allows to call specific values within that date    
                    'year'  => $regdate['year'],
                    'month' => $regdate['month'],
                    'day'   => $regdate['day'],
                ),
                # Include posts from the day the user registered  
                'inclusive' => true,
            ),
        ),
        # Display all posts on a single page.
    );          
    $my_query = new WP_Query( $args );
    while ($my_query->have_posts()) : $my_query->the_post(); 

        get_template_part( 'template-parts/content', get_post_format() );

    endwhile; ?>
    <?php
        /**
         * Fix Pagination in custom page/archive
         * Set global wp_query the same as our custom query
         * 
         * Use function the_posts_pagination( $args ); for pagination will work too
         * see https://developer.wordpress.org/reference/functions/get_the_posts_pagination/#source-code
        */
        $GLOBALS['wp_query'] = $my_query;
    ?>
    <div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
    <div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>

<?php else: ?>
    Nada
<?php endif; ?>
<?php wp_reset_query(); ?> // MUST use to reset global query and post data

另一种方法仍然不建议使用wp_reset_query()进行自定义查询。有关它的更多信息,您可以在query_post中了解更多信息。