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

我最近有很多帮助创建即将发生的事件列表(见这里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 ) ); } ?>

解决方法:

终于解决了这个:

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

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...