还可以进行筛选\'month_link\'
对于月度档案,\'year_link\'
用于年度档案和\'day_link\'
用于日常存档。
您还可以编写一个扩展wp_get_archives
要使用CPT,请将筛选器添加到\'getarchives_where\'
和存档链接。
function wp_get_cpt_archives( $cpt = \'post\', $args = array() ) {
// if cpt is post run the core get archives
if ( $cpt === \'post\' ) return wp_get_archives($args);
// if cpt doesn\'t exists return error
if ( ! post_type_exists($cpt) ) return new WP_Error(\'invalid-post-type\');
$pto = get_post_type_object($cpt);
// if cpt doesn\'t have archive return error
if ( ! $pto = get_post_type_object( $cpt ) ) return false;
$types = array(\'monthly\' => \'month\', \'daily\' => \'day\', \'yearly\' => \'year\');
$type = isset( $args[\'type\'] ) ? $args[\'type\'] : \'monthly\';
if ( ! array_key_exists($type, $types) ) {
// supporting only \'monthly\', \'daily\' and \'yearly\' archives
return FALSE;
}
$done_where = $done_link = FALSE;
// filter where
add_filter( \'getarchives_where\' , function( $where ) use (&$done_where, $cpt) {
if ( $done_where ) return $where;
return str_replace( "post_type = \'post\'" , "post_type = \'{$cpt}\'" , $where );
});
// filter link
add_filter( "{$types[$type]}_link", function( $url ) use (&$done_link, $pto, $cpt) {
if ( $done_link ) return $url;
// if no pretty permalink add post_type url var
// if ( get_option( \'permalink_structure\' ) || ! $pto->rewrite ) {
if ( ! get_option( \'permalink_structure\' ) || ! $pto->rewrite ) {
return add_query_arg( array( \'post_type\' => $cpt ), $url );
} else { // pretty permalink
global $wp_rewrite;
$slug = is_array( $pto->rewrite ) ? $pto->rewrite[\'slug\'] : $cpt;
$base = $pto->rewrite[\'with_front\'] ? $wp_rewrite->front : $wp_rewrite->root;
$home = untrailingslashit( home_url( $base ) );
return str_replace( $home, home_url( $base . $slug ), $url );
}
});
// get original echo arg and then set echo to false
$notecho = isset($args[\'echo\']) && empty($args[\'echo\']);
$args[\'echo\'] = FALSE;
// get archives
$archives = wp_get_archives($args);
// prevent filter running again
$done_where = $done_link = TRUE;
// echo or return archives
if ( $notecho ) {
return $archives;
} else {
echo $archives;
}
}
有关函数如何工作的更多信息,请阅读内联注释。
现在有一个问题。url类似/events/2014/04/
是not WordPress可以识别,因此您需要添加处理此类URL的reqrite规则。
同样,您可以编写一个函数来为您添加规则:
function generate_cpt_archive_rules( $cpt ) {
if ( empty($cpt) || ! post_type_exists($cpt) ) return;
global $wp_rewrite;
$pto = get_post_type_object($cpt);
if ( ! $pto->has_archive ) return;
$base = $pto->rewrite[\'with_front\'] ? $wp_rewrite->front : $wp_rewrite->root;
$base = trailingslashit( $base );
$slug = is_array( $pto->rewrite ) ? $pto->rewrite[\'slug\'] : $cpt;
$year = ltrim( $base . $slug . \'/([0-9]{4})/?$\', \'/\' );
$month = ltrim( $base . $slug . \'/([0-9]{4})/([0-9]{2})/?$\', \'/\' );
$day = ltrim( $base . $slug . \'/([0-9]{4})/([0-9]{2})/([0-9]{2})/?$\', \'/\' );
$index = \'index.php?post_type=\' . $cpt;
$rules[$year] = $index . \'&m=$matches[1]\';
$rules[$month] = $index . \'&m=$matches[1]$matches[2]\';
$rules[$day] = $index . \'&m=$matches[1]$matches[2]$matches[3]\';
$page = 2;
foreach ( array( $year, $month, $day ) as $rule ) {
$paged = str_replace( \'/?$\', \'/page/([0-9]{1,})/?$\', $rule);
$rules[$paged] = $rules[$rule] . \'&paged=$matches[\' . $page . \']\';
$page++;
}
return $rules;
}
此函数生成规则,但是,您还需要添加和刷新规则。
现在建议刷新主题(或插件)激活的规则并在init上注册,因此您应该执行以下操作:
function register_cpt_dates_rules() {
$cpts = array( \'events\' );
foreach ( $cpts as $cpt ) {
foreach ( generate_cpt_archive_rules( $cpt ) as $rule => $rewrite ) {
add_rewrite_rule( $rule, $rewrite, \'top\' );
}
}
}
// flushing on theme switch
add_action(\'after_switch_theme\', function() {
register_cpt_dates_rules();
$GLOBALS[\'wp_rewrite\']->flush_rules();
});
// registering on init
add_action( \'init\', \'register_cpt_dates_rules\' );
将这些函数添加到
functions.php
(您已停用并再次激活您的主题
\'after_switch_theme\'
运行)您可以使用以下方式显示存档:
wp_get_cpt_archives( \'events\' );
它将显示事件CPT的档案,链接将是
/events/2014/04/
.
它还适用于年度或每日存档:
wp_get_cpt_archives( \'events\', array( \'type\'=>\'yearly\' ) );
wp_get_cpt_archives( \'events\', array( \'type\'=>\'daily\' ) );
使用我的两个函数,您可以非常轻松地
wp_get_archives
与CPT配合使用。
请注意all 这里的代码需要PHP 5.3+。