【问题标题】:Pagination showing same posts from page 1 on all other pages分页在所有其他页面上显示第 1 页的相同帖子
【发布时间】:2013-06-27 16:08:51
【问题描述】:

我最近在创建即将发生的事件列表时得到了很多帮助(请参阅此处Showing upcoming events (including todays event)?),结果我使用 WP Pagenavi 的分页功能被破坏了。

目前,当您单击第 2 页时,它只会显示与第 1 页相同的帖子。虽然 URL 实际上确实更改为 page/2 page/3 等。

我的functions.php文件中有这个:

function filter_where( $where = '' ) {
    $where .= " AND post_date >= '" . date("Y-m-d") . "'";
    return $where;
}

add_filter( 'posts_where', 'filter_where' );

$query = new WP_Query(
    array(
        'post__not_in' => array(4269),
        'paged' => get_query_var('paged'),
        'post_type' => 'whatson',
        'exclude' => '4269',
        'post_status' => 'future,publish',
        'posts_per_page' => 20,
        'order' => 'ASC'
    )
);

remove_filter( 'posts_where', 'filter_where' );

我的循环如下:

<?php while ( $query->have_posts() ) : $query->the_post(); ?>
// content
<?php endwhile; // end of the loop.  ?>
<?php if (function_exists('wp_pagenavi')) { wp_pagenavi( array( 'query' => $query ) ); } ?>

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    终于解决了这个问题:

    function my_filter_where( $where = '' ) {
        global $wp_query;
        if (is_array($wp_query->query_vars['post_status'])) {
    
            if (in_array('future',$wp_query->query_vars['post_status'])) {
            // posts today into the future
            $where .= " AND post_date > '" . date('Y-m-d', strtotime('now')) . "'";
            }
        }
        return $where;
    }
    add_filter( 'posts_where', 'my_filter_where' );
    

    还有:

    <?php
    $wp_query = array(
            'post__not_in' => array(4269),
            'paged' => get_query_var('paged'),
            'post_type' => 'whatson',
            'exclude' => '4269',
            'posts_per_page' => 20,
            'order' => 'ASC',
            'orderby' => 'date',
            'post_status' =>array('future','published'));
    query_posts($wp_query);
    ?>
    
    <?php 
    if ($wp_query->have_posts()) {
        while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
            Content
        <?php endwhile; // end of the loop.
    } ?>
    
    <?php if (function_exists('wp_pagenavi')) { wp_pagenavi( array( 'query' => $wp_query ) ); } ?>
    

    【讨论】:

    • 顺便说一句。它首先不起作用的原因是因为在静态页面上输出帖子时必须使用get_query_var('page')而不是get_query_var('paged')
    • @n1te 不是,两者之间的变化没有区别。
    【解决方案2】:

    您希望它用于特定帖子还是所有帖子?如果您想要一般分页,您可以使用这段代码制作没有插件的分页链接:

    <?php
     global $wp_query;
    $big = 999999999; // need an unlikely integer
    echo paginate_links( array(
     'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
     'format' => '?paged=%#%',
     'current' => max( 1, get_query_var('paged') ),
     'total' => $wp_query->max_num_pages
     ) );
     ?>
    

    只需将它添加到您的 index.php 或 archives.php 中,看看奇迹发生了 :)

    【讨论】:

    • 我想要他们所有人。如果可能的话,我想尝试让它与我所拥有的一起工作。
    【解决方案3】:

    我不确定您的其余代码发生了什么,但可以尝试的一个简单测试是在您的new WP_Query 之前使用wp_reset_query(),以确保查询变量具有未修改。

    【讨论】:

    • 我在几个不同的地方尝试过,但现在运气不错。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-29
    • 2018-08-23
    • 2017-11-10
    • 1970-01-01
    相关资源
    最近更新 更多