分页显示基于日期的archive.php循环的空白页 [英] Pagination shows empty pages for date based archive.php loop

查看:76
本文介绍了分页显示基于日期的archive.php循环的空白页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用wordpress,并具有用于自定义帖子类型的自定义存档页面. 自定义循环获取已登录用户的注册日期,并且仅显示在他们注册时或注册后发布的帖子.效果很好.

I'm using wordpress and have a custom archive page for a custom post type. The custom loop gets the logged in users registration date and only shows posts that were published on or after they registered. Works great.

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

However, the pagination is still using the main query so if there are 4 posts in total set to 2 posts per page the pagination always shows there are two pages even if only one post is displayed due to the logged in users registered date.

任何人都可以帮助我修改我拥有的内容,以便分页仅显示针对该用户查询的2个以上帖子中的结果吗?我已经尝试了好几个小时,使用在网络上找到的各种更改...

Can anyone help me modify what I have so the pagination only shows for results in more than 2 posts for that users query? I've been trying for hours now using various changes I've found on the web...

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

推荐答案

使用自定义存档和分页

@Scott Eldo 创建自定义查询的方法不会更改自定义档案库中的主查询.仅供参考,您是正确的,分页仅适用于主要查询.

Working with Custom Archive and Pagination

@Scott Eldo Your approach with create custom query will not change main query on your custom archive. FYI and you are correct, pagination only work for main query.

在您的情况下,推荐的方法是使用过滤器 pre_get_posts 与自定义存档和分页一起使用.请在这里如何在帖子类型存档页面上修改主查询,您可以通过以下方式查看我的答案:您的查询参数.

In your case, the recommended approach, I will use filter pre_get_posts to work with custom archive and pagination. Please take a look my answer here how to modify main query on post type archive page, you can figure it out with your query parameters.

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

BUT if you intent to create query direct into your template ( even it is not change main query ), you need to match your custom query with $GLOBALS['wp_query'] that use in pagination and don't forget to use wp_reset_query() ( MUST ). Take a look my approach here related with your code:

<?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 .有关此内容的更多信息,您可以在此处了解更多信息.

Another approach and still NOT recommended is use query_post for your custom query. More reading about it you can learn more in here.

这篇关于分页显示基于日期的archive.php循环的空白页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆