如何使用wordpress进行“日期分页”

时间:2018-01-28 10:49:23

标签: php wordpress pagination

我的wordpress网站上有这个代码。

$today = date("Y-m-d"); //Today's posts
        $args = array( 
                    'post_type' => 'questions', 
                    'meta_key' => '_question_date', 
                    'posts_per_page' => 20, 
                    'order' => 'DESC',
                    'meta_query' => array(
                        array(
                           'key' => '_question_date',
                           'value' => $today,
                           'compare' => '=',
                           'type' => 'CHAR'
                       )
                    )
 );

我可以使用自定义元查询日期对此进行分页吗?请帮帮我!

2 个答案:

答案 0 :(得分:3)

您可以使用Date Pagination plugin 您可以在安装后使用主题中的插件功能

<强>实施例

这些示例显示如何通过设置date_pagination_type查询变量向页面添加日期分页。

使用pre_get_posts进行日期分页

您可以使用“date_pagination_type”查询变量将日期分页类型设置为“年度”,“每月”或“每日”。

add_action( 'pre_get_posts', 'monthly_paginated_home_query' );
function monthly_paginated_home_query( $query ) {

    // not a wp-admin page and the query is for the main query
    if ( !is_admin() && $query->is_main_query() ) {

        //  on the home page only
        if ( is_home() ) {

            // set the date pagination to 'monthly'
            $query->set('date_pagination_type', 'monthly'); 

            // set other arguments here
        }
    }
}

与WP_Query的日期分页

您可以使用“date_pagination_type”参数将日期分页的类型设置为“年度”,“每月”或“每日”。

在此示例中,我们将其设置为“每月”。

<?php
// Get the paged variable and use it in the custom query.
// (see: http://codex.wordpress.org/Pagination ).
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

// Example arguments.
$args = array(
    // set the date pagination to monthly
    'date_pagination_type' => 'monthly', // 'yearly', 'monthly', 'daily'
    'paged'                => $paged,
);

// The custom query.
$the_query = new WP_Query( $args );
?>
<!-- The Loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<!-- Your theme's loop code here -->
<?php endwhile; ?>

答案 1 :(得分:1)

这是我的解决方案。它运作得很好。我希望这对别人有所帮助。

            $today = date("Y-m-d");
            $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
                if($paged == 1) {
                    $getdate = $today;
                } 
                else if ($paged >= 2) {
                    $date = strtotime("-".$paged+1 ." days");
                    $getdate = date("Y-m-d",$date);
                }
            $args = array( 
                        'post_type' => 'questions', 
                        'meta_key' => '_question_date', 
                        'posts_per_page' => 20, 
                        'order' => 'DESC',
                        'meta_query' => array(
                            array(
                               'key' => '_question_date',
                               'value' => $getdate,
                               'compare' => '=', 
                               'type' => 'CHAR'
                           )
                        )
            );