这不是完整的复制/粘贴代码,但希望它足够容易理解,以便您开始使用。
第一步是注册您的帖子类型,并添加重写规则来处理年/月。这将为您提供event/post-name/
, 您的帖子类型存档位于calendar
, 并处理传入的请求calendar/yyyy/mm/
. 确保在添加后访问设置>永久链接页面以刷新重写规则。
function wpa88173_calendar_post_type() {
// just the important bits shown here
$args = array(
\'rewrite\' => array( \'slug\' => \'event\' )
\'has_archive\' => \'calendar\',
);
register_post_type( \'calendar\', $args );
add_rewrite_rule(
\'^calendar/([0-9]{4})/([0-9]{2})/?\',
\'index.php?post_type=calendar&calendar_year=$matches[1]&calendar_month=$matches[2]\',
\'top\'
);
}
add_action( \'init\', \'wpa88173_calendar_post_type\' );
下一步是添加
calendar_year
和
calendar_month
查询变量,因此WordPress在解析传入请求时将它们添加到查询变量数组中。
function wpa88173_calendar_query_vars( $query_vars ) {
$query_vars[] = \'calendar_year\';
$query_vars[] = \'calendar_month\';
return $query_vars;
}
add_filter(\'query_vars\', \'wpa88173_calendar_query_vars\' );
下一步是将操作添加到
pre_get_posts
, 它检查是否是日历后期类型的存档,获取年/月或将其设置为当前年/月,然后使用
meta_query
用于加载请求的年/月的参数。看见
WP_Query
有关元查询的更多信息。假设日期格式为
yyyymmdd
.
function wpa88173_calendar_query( $query ) {
// is it a post type archive?
if( ! $query->is_post_type_archive( \'calendar\' ) )
return;
// is it the main query and not an admin page?
if( $query->is_main_query()
&& ! is_admin() ) {
// check if year/month was set via the URI, or set it to current year/month
( ! empty( $query->query_vars[\'calendar_year\'] ) ) ? $query_year = $query->query_vars[\'calendar_year\'] : $query_year = date(\'Y\');
( ! empty( $query->query_vars[\'calendar_month\'] ) ) ? $query_month = $query->query_vars[\'calendar_month\'] : $query_month = date(\'m\');
// meta_query parameters for events between start and end dates
$date_start = $query_year . $query_month . \'01\';
$date_end = $query_year . $query_month . \'31\';
$meta_query = array(
array(
\'key\' => \'event_date\',
\'value\' => array( $date_start, $date_end ),
\'compare\' => \'BETWEEN\',
\'type\' => \'NUMERIC\'
)
);
// modify the query
$query->set( \'meta_key\', \'event_date\' );
$query->set( \'orderby\', \'meta_value_num\' );
$query->set( \'order\', \'ASC\' );
$query->set( \'meta_query\', $meta_query );
}
}
add_action( \'pre_get_posts\', \'wpa88173_calendar_query\' );
最后一步是在模板中构建日历,并创建下一个/上一个链接,以浏览月份。您可以通过以下方式在模板中获取查询的年/月:
get_query_var
.
EDIT - 下面是一个用普通数学建立链接的示例
( \'\' == get_query_var( \'calendar_month\' ) ) ? $this_month = date( \'n\' ) : $this_month = ltrim( get_query_var( \'calendar_month\' ), \'0\' );
( \'\' == get_query_var( \'calendar_year\' ) ) ? $this_year = date( \'Y\' ) : $this_year = get_query_var( \'calendar_year\' );
if( 1 == $this_month ):
$next_month = 2;
$prev_month = 12;
$next_year = $this_year;
$prev_year = $this_year - 1;
elseif( 12 == $this_month ):
$next_month = 1;
$prev_month = 11;
$next_year = $this_year + 1;
$prev_year = $this_year;
else:
$next_month = $this_month + 1;
$prev_month = $this_month - 1;
$next_year = $this_year;
$prev_year = $this_year;
endif;
$next_month = str_pad( $next_month , 2, \'0\', STR_PAD_LEFT );
$prev_month = str_pad( $prev_month , 2, \'0\', STR_PAD_LEFT );
echo \'next month: /calendar/\' . $next_year . \'/\' . $next_month . \'/\';
echo \'previous month: /calendar/\' . $prev_year . \'/\' . $prev_month . \'/\';