本教程将详细介绍如何将子比主题中自定义的帖子标签(forum_tag)与话题(forum_topic)分类法的链接从基于slug的格式改为基于term_id的数字格式,使其URL更加简洁统一,也是为了更好的SEO效果和用户体验。
⚠️友情提示:修改前请做好备份!
运行环境: php版本 :7.4+(目前测试到的版本最高在8.4,其他版本请自行测试) WordPress版本 :6.8.2 子比主题版本 :V8.1
效果展示
/* 帖子话题URL地址 */
https://www.example.com/forum_topic/话题中文字符 => https://www.example.com/forum_topic/123.html
/* 帖子标签URL地址 */
https://www.example.com/forum_tag/标签中文字符 => https://www.example.com/forum_tag/123.html
直接将核心代码整合到主题的func.php中
<?php
/**
* 将帖子话题(forum_topic)和帖子标签(forum_tag)链接从基于slug的格式改为基于term_id的数字格式
* 添加到主题根目下的func.php文件中(没有的自己新建一个func.php文件)
* 如果希望链接末尾带 .html ,请取消注释最后一行代码,反之同理
*/
class Zib_Term_Links_Modifier {
private static $instance = null;
public static function get_instance() {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
private function __construct() {
$this->setup_hooks();
}
private function setup_hooks() {
// 注册过滤器和动作
add_action('init', array($this, 'add_rewrite_rules'));
add_filter('term_link', array($this, 'forum_tag_term_link'), 10, 3);
add_filter('term_link', array($this, 'forum_topic_term_link'), 10, 3);
add_filter('request', array($this, 'numeric_term_request'));
// 主题激活时刷新重写规则
add_action('after_switch_theme', array($this, 'maybe_flush_rewrite_rules'));
}
/**
* 修改forum_tag的链接为term_id格式
*/
public function forum_tag_term_link($link, $term, $taxonomy) {
if ($taxonomy === 'forum_tag') {
return home_url("/forum_tag/{$term->term_id}");
}
return $link;
}
/**
* 修改forum_topic的链接为term_id格式
*/
public function forum_topic_term_link($link, $term, $taxonomy) {
if ($taxonomy === 'forum_topic') {
return home_url("/forum_topic/{$term->term_id}");
}
return $link;
}
/**
* 处理数字ID的查询解析
*/
public function numeric_term_request($query_vars) {
$taxonomies = ['forum_tag', 'forum_topic'];
foreach ($taxonomies as $taxonomy) {
if (isset($query_vars[$taxonomy]) && is_numeric($query_vars[$taxonomy])) {
$term = get_term($query_vars[$taxonomy], $taxonomy);
if ($term && !is_wp_error($term)) {
$query_vars[$taxonomy] = $term->slug;
}
}
}
return $query_vars;
}
/**
* 添加重写规则
*/
public function add_rewrite_rules() {
// forum_topic重写规则
add_rewrite_rule('^forum_topic/([0-9]+)/?$', 'index.php?forum_topic=$matches[1]', 'top');
add_rewrite_rule('^forum_topic/([0-9]+)/page/([0-9]{1,})/?$', 'index.php?forum_topic=$matches[1]&paged=$matches[2]', 'top');
// forum_tag重写规则
add_rewrite_rule('^forum_tag/([0-9]+)/?$', 'index.php?forum_tag=$matches[1]', 'top');
add_rewrite_rule('^forum_tag/([0-9]+)/page/([0-9]{1,})/?$', 'index.php?forum_tag=$matches[1]&paged=$matches[2]', 'top');
}
/**
* 刷新重写规则
*/
public function maybe_flush_rewrite_rules() {
if (get_option('zib_term_links_rewrite_flushed') !== '1') {
flush_rewrite_rules();
update_option('zib_term_links_rewrite_flushed', '1');
}
}
/**
* 可选:添加.html后缀版本
*/
public function enable_html_suffix() {
remove_filter('term_link', array($this, 'forum_tag_term_link'));
remove_filter('term_link', array($this, 'forum_topic_term_link'));
add_filter('term_link', array($this, 'forum_tag_term_link_html'), 10, 3);
add_filter('term_link', array($this, 'forum_topic_term_link_html'), 10, 3);
// 更新重写规则
remove_action('init', array($this, 'add_rewrite_rules'));
add_action('init', array($this, 'add_rewrite_rules_html'));
}
public function forum_tag_term_link_html($link, $term, $taxonomy) {
if ($taxonomy === 'forum_tag') {
return home_url("/forum_tag/{$term->term_id}.html");
}
return $link;
}
public function forum_topic_term_link_html($link, $term, $taxonomy) {
if ($taxonomy === 'forum_topic') {
return home_url("/forum_topic/{$term->term_id}.html");
}
return $link;
}
public function add_rewrite_rules_html() {
add_rewrite_rule('^forum_topic/([0-9]+)\.html/?$', 'index.php?forum_topic=$matches[1]', 'top');
add_rewrite_rule('^forum_topic/([0-9]+)\.html/page/([0-9]{1,})/?$', 'index.php?forum_topic=$matches[1]&paged=$matches[2]', 'top');
add_rewrite_rule('^forum_tag/([0-9]+)\.html/?$', 'index.php?forum_tag=$matches[1]', 'top');
add_rewrite_rule('^forum_tag/([0-9]+)\.html/page/([0-9]{1,})/?$', 'index.php?forum_tag=$matches[1]&paged=$matches[2]', 'top');
}
}
// 初始化类
Zib_Term_Links_Modifier::get_instance();
// 可选:如果需要.html后缀,取消注释下面这行
// Zib_Term_Links_Modifier::get_instance()->enable_html_suffix();
© 版权声明
THE END
暂无评论内容