繁体   English   中英

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

[英]How can i make “date pagination” with wordpress

我在我的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'
                       )
                    )
 );

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

您可以使用日期分页插件在安装后可以在主题中使用插件功能

例子

这些示例向您展示了如何通过设置date_pagination_type查询变量来向页面添加Date Pagination。

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

这是我的解决方案。 它工作得很好。 我希望这对其他人有帮助。

            $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'
                           )
                        )
            );

暂无
暂无

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

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