如果我理解你的问题,你希望所有档案(类别、标签、作者…)都是这样的您希望在第1页显示与存档相关但仅在当月发布的帖子。
然后,您需要链接“下一页”和“上一页”,分别显示下一个月和上一个月的帖子。
您链接的ansqwer为您提供了很大帮助,您必须对其进行优化,以便在每个连接pre_get_post
行动
我的建议是使用m
参数设置为所需的月份,然后设置posts_per_page = -1
.
这将强制显示所需月份的所有帖子,使用导航可以按月浏览。
然后,您需要一些逻辑来创建第一页(如果没有page
在url中)显示上次发布帖子当月的所有页面。
之后,您可以创建几个函数来帮助您输出导航URL。
我创建了一个简单的插件来处理我所说的内容:
<?php
/**
* Plugin Name: GM Archives By Month
* Plugin URI: http://wordpress.stackexchange.com/questions/112475/limit-to-months-rather-than-by-number-of-posts
* Description: A plugin that makes all archives paginating by months and provides functions to show pagination links
* Author: G.M.
*/
class GMArchivesByMonth {
static $has_pretty = false;
static $last_date;
static function init() {
add_filter( \'init\', array(__CLASS__, \'sniff_pretty\'), 1 );
}
static function sniff_pretty($vars) {
global $wp_rewrite;
self::$has_pretty = ( empty($wp_rewrite->get_page_permastruct()) ) ? false : true;
}
static function get_last_post_date( $wp_query = \'\' ) {
$args = $wp_query->query_vars;
$args = wp_parse_args( array(\'posts_per_page\' => 1, \'paged\' => 1, \'orderby\' => \'date\', \'order\' => \'DESC\'), $args);
remove_action(\'pre_get_posts\', \'setGMArchivesByMonthPreQuery\', 10, 1);
$posts = get_posts( $args );
add_action(\'pre_get_posts\', \'setGMArchivesByMonthPreQuery\', 10, 1);
if ( empty($posts) ) return false;
$lastPost = array_shift( $posts );
return $lastPost->post_date;
}
static function get_my_from_paged( $paged = 1 ) {
$date = self::$last_date;
$m = intval( mysql2date(\'n\', self::$last_date) ) - $paged + 1;
$y = intval( mysql2date(\'Y\', self::$last_date) );
if ( $m < 1) { $m = 12; $y--; }
if ( $m < 9) { $m = \'0\' . $m; }
return "$y$m";
}
static function change_paged_for_month( $query ) {
if ( is_main_query() && ( ( is_archive() && ! is_date() ) || is_home() ) ) {
$paged = $query->get(\'paged\') ? : 1;
self::$last_date = self::get_last_post_date( $query );
if ( ! self::$last_date ) return;
if ( $paged == 1 ) {
// if no month in query set the query to month of last post
$m = mysql2date(\'m\', self::$last_date );
$y = mysql2date(\'Y\', self::$last_date );
$my = "$y$m";
} else {
$my = self::get_my_from_paged($paged);
}
// set m query var to current request
$query->set( \'m\', $my );
// set posts per page -1 so all posts in required month are shown
$query->set(\'posts_per_page\', -1);
}
}
protected static function get_month_paged_url( $i = \'\' ) {
$paged = get_query_var(\'paged\') ? : 1;
if ( $paged == 1 && $i == -1 ) return false;
$paged += $i;
/* ----------------------------------------
* This lines make page loading slower,
* but prevent the \'Next Page\' link is showed whe there are no posts on previous month
*/
$my = self::get_my_from_paged( $paged );
global $wp_query;
$args = $wp_query->query_vars;
$args = wp_parse_args( array(\'posts_per_page\' => 1, \'paged\' => 1, \'orderby\' => \'date\', \'order\' => \'DESC\', \'m\' => $my), $args);
remove_action(\'pre_get_posts\', \'setGMArchivesByMonthPreQuery\', 10, 1);
$posts = get_posts( $args );
add_action(\'pre_get_posts\', \'setGMArchivesByMonthPreQuery\', 10, 1);
if ( empty($posts) ) return false;
/* ---------------------------------------- */
if ( ! self::$has_pretty ) {
return add_query_arg( array( \'paged\' => $paged) );
} elseif ( substr_count($_SERVER[\'REQUEST_URI\'], "page/") ) {
return preg_replace(\'/(page\\/)([0-9])+/\', "page/$paged", $_SERVER[\'REQUEST_URI\']);
} else {
return trailingslashit($_SERVER[\'REQUEST_URI\']) . "page/$paged";
}
}
static function get_previous_month_paged_url() { return self::get_month_paged_url( -1 ); }
static function get_next_month_paged_url() { return self::get_month_paged_url( 1 ); }
static function get_previous_month_paged_link( $text = \'\' ) {
$url = self::get_previous_month_paged_url();
if ( ! $url ) return \'\';
if ( empty( $text) ) $text = __(\'Previous Page\');
return \'<a href="\' . esc_url( $url ) . \'">\' . esc_html($text) . \'</a>\';
}
static function get_next_month_paged_link( $text = \'\' ) {
$url = self::get_next_month_paged_url();
if ( ! $url ) return \'\';
if ( empty( $text) ) $text = __(\'Next Page\');
return \'<a href="\' . esc_url( $url ) . \'">\' . esc_html($text) . \'</a>\';
}
static function previous_month_paged_link( $text = \'\' ) { echo self::get_previous_month_paged_link($text); }
static function next_month_paged_link( $text = \'\' ) { echo self::get_next_month_paged_link($text); }
}
// Class init on init action
add_action(\'after_setup_theme\', array(\'GMArchivesByMonth\', \'init\') );
// function hooking pre_get_posts in a easy removable way
function setGMArchivesByMonthPreQuery( $query ) {
GMArchivesByMonth::change_paged_for_month( $query );
}
add_action(\'pre_get_posts\', \'setGMArchivesByMonthPreQuery\');
// Helpers
function previous_page_month() { GMArchivesByMonth::previous_month_paged_link(); }
function next_page_month() { GMArchivesByMonth::next_month_paged_link(); }
代码中的注释将帮助您更好地理解插件的功能。
将插件保存在插件目录下的文件中并激活它。
之后,在处理归档文件的模板文件中,如“主页”。php“,”分类法。php“,”类别。php等将此代码用于显示导航链接:
<p><?php previous_page_month() ?> | <?php next_page_month(); ?></p>
在索引中。php您可以将其封装在if语句中,以防止在不受支持的页面中显示链接,如单一视图或基于日期的存档:
<?php if ( ( is_archive() && ! is_date() ) || is_home() ) { ?>
<p><?php previous_page_month() ?> | <?php next_page_month(); ?></p>
<?php } ?>
请注意,分页链接是由插件根据permalink结构生成的。在没有有效的永久链接的情况下,链接将使用url变量。