【问题标题】:Pagination shows empty pages for date based archive.php loop分页显示基于日期的archive.php循环的空白页面
【发布时间】:2016-07-29 22:57:03
【问题描述】:

我正在使用 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; ?>

【问题讨论】:

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


    【解决方案1】:

    使用自定义存档和分页

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

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

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

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

    另一种仍然不推荐的方法是使用query_post 进行自定义查询。有关它的更多信息,您可以在here 中了解更多信息。

    【讨论】:

    • 这是正确的!非常感谢您的时间和帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-26
    • 2013-04-08
    • 1970-01-01
    • 2013-06-04
    • 2013-03-03
    • 2011-12-27
    • 1970-01-01
    相关资源
    最近更新 更多