分类
wordpress 分类和作者页面链接按ID显示方法
/**
*Change category,author slug to ID
*
**/
// 修改分类链接结构
function change_category_link($termlink, $term, $taxonomy) {
if ($taxonomy == 'category') {
return home_url('category/' . $term->term_id);
}
return $termlink;
}
add_filter('term_link', 'change_category_link', 10, 3);
// 生成分类的自定义重写规则
function custom_category_rewrite_rules($wp_rewrite) {
$new_rules = array(
'category/(\d+)$' => 'index.php?cat=$matches[1]'
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'custom_category_rewrite_rules');
// 在保存分类时刷新重写规则
function refresh_category_rewrite_rules() {
flush_rewrite_rules();
}
add_action('created_category', 'refresh_category_rewrite_rules', 10, 2);
add_action('edited_category', 'refresh_category_rewrite_rules', 10, 2);
// 修改作者链接结构
function change_author_link($link, $author_id, $author_nicename) {
return home_url('author/' . $author_id);
}
add_filter('author_link', 'change_author_link', 10, 3);
// 生成作者的自定义重写规则
function custom_author_rewrite_rules($wp_rewrite) {
$new_rules = array(
'author/(\d+)$' => 'index.php?author=$matches[1]'
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'custom_author_rewrite_rules');
// 在保存作者时刷新重写规则
function refresh_author_rewrite_rules() {
flush_rewrite_rules();
}
add_action('profile_update', 'refresh_author_rewrite_rules', 10, 2);
add_action('user_register', 'refresh_author_rewrite_rules', 10, 2);