繁体   English   中英

分页在所有其他页面上显示来自第1页的相同帖子

[英]Pagination showing same posts from page 1 on all other pages

我最近有很多帮助创建即将发生的事件列表(见这里显示即将发生的事件(包括今天的事件)? ),结果我使用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 ) ); } ?>

终于解决了这个:

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

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

<?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,看看魔术发生:)

我不确定你的代码的其余部分是怎么回事,但是尝试一个简单的测试就是在你的new WP_Query之前使用wp_reset_query()来确保查询变量没有被修改。

暂无
暂无

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

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