是的,norixxx,你上面的评论是正确的答案:
add_filter(\'year_link\', \'change_year_link\', 10, 2);
function change_year_link($yearlink, $year){
return home_url(user_trailingslashit(\'news/\'.$year));
}
谢谢分享!
在下面,用户可以找到“扩展”版本:
// generate /year, /year/month, /year/month/post-slug rules
add_action(\'generate_rewrite_rules\', \'rewrite_rules\');
function rewrite_rules($wp_rewrite){
$rules = array();
$rules[\'news/([0-9]{4})/?$\'] = \'index.php?year=$matches[1]\';
$rules[\'news/([0-9]{4})/([0-9]{2})/?$\'] = \'index.php?year=$matches[1]&monthnum=$matches[2]\';
$rules[\'news/([0-9]{4})/([0-9]{2})/([^/]+)/?$\'] = \'index.php?year=$matches[1]&monthnum=$matches[2]&name=$matches[3]\';
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter(\'year_link\', \'change_year_link\', 10, 2);
function change_year_link($yearlink, $year){
return home_url(user_trailingslashit(\'news/\'.$year));
}
add_filter(\'month_link\', \'change_month_link\', 10, 2);
function change_month_link($yearlink, $year, $month){
return home_url(user_trailingslashit(\'news/\'.$year.\'/\'.$month));
}
// this last is for generating /year/month/post-slug links
// I think it can be optional. Keep in mind that by applying this filter,
// You won\'t be able to manually modify the slug in admin edit post view
add_filter(\'post_link\', \'set_post_links\' , 10, 2);
function set_post_links($permalink, $post){
if(is_wp_error($post) || empty($post->post_name)) return $permalink;
if(\'post\' == $post->post_type){
$year = get_the_date(\'Y\', $post->ID);
$month = get_the_date(\'m\', $post->ID);
return home_url(user_trailingslashit("$year/$month/$post->post_name"));
}
return $permalink;
}
// If you use the post_link filter,
// probably you\'ll want to auto-update post slugs upon post title modifications.
// Here\'s how:
add_filter(\'wp_insert_post_data\', \'update_post_data\' , 10, 2);
function update_post_data($data, $postarr){
// protect non-post posts
if($data[\'post_type\'] !== \'post\') return $data;
$data[\'post_name\'] = wp_unique_post_slug(sanitize_title($data[\'post_title\']), $postarr[\'ID\'], $data[\'post_status\'], $data[\'post_type\'], $data[\'post_parent\']);
return $data;
}