对于WordPress网站,尤其是使用WordPress子比主题并高度依赖其论坛或社区功能的站点来说,URL结构的优化对于SEO和用户体验至关重要。默认情况下,WordPress自定义分类法的链接通常基于分类的slug(别名),这在中文环境下可能会导致URL过长、不美观,甚至包含编码字符。将这些链接改为基于term_id的数字格式,不仅能使URL更加简洁、统一,还能在一定程度上提升搜索引擎的友好度,并提供更好的用户体验。
核心需求
将WordPress子比主题中自定义的帖子标签(forum_tag)和话题(forum_topic)分类法的链接格式进行优化,具体表现为:
帖子话题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
主题铺提醒: 在进行任何网站核心文件或配置更改之前,务必做好全面的网站备份(包括数据库和文件),以防万一出现不可预料的问题。
运行环境要求:
PHP版本: 7.4+ (已在PHP8.4上进行测试,其他新版本请自行测试兼容性)
WordPress版本: 6.8.2 (建议在最新稳定版WordPress上运行)
WordPress子比主题版本: V8.1 (确保您的WordPress子比主题是最新版本,以获得最佳兼容性)
SEO代码
直接将核心代码整合到主题的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();以下是实现原理,可看可不看
核心代码实现
首先,需要修改的WordPress子比主题源码:子比主题目录/inc/functions/bbs/inc/class.init.php 。修改后如果主题更新,代码会被覆盖,需要重新修改。如果说你想要该功能且还能支持主题更新,可以把代码转到主题的func.php文件中,不过我懒得去写去测试了。
第一步:注册必要的过滤器
// 在 public function setup() 函数内添加下面代码
// 添加 forum_tag 和 forum_topic 链接过滤器
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'));
// 直接找 new zib_bbs_admin($this); 在这段代码上方添加第二步:添加链接生成函数
// 在 public function setup() 函数结束后面新增下面两个函数
/**
* 添加新函数
* 将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}.html");
}
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}.html");
}
return $link;
}第三步:处理数字ID的查询解析
// 紧接着第二步后面添加下面这个新函数
/**
* @description: 处理数字ID的查询解析(同时处理 forum_tag 和 forum_topic)
* @param {*} $query_vars
* @return {*}
*/
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_rule() 函数最后位置添加下面代码
// 添加 forum_topic 基于数字ID的重写规则
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');
// 添加 forum_tag 基于数字ID的重写规则
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');第五步:配置帖子话题和标签分类法注册参数
// 注册forum_topic分类法时设置rewrite参数
'rewrite' => array(
'slug' => 'forum_topic',
'with_front' => false,
'hierarchical' => false
),
// 找到 register_taxonomy('forum_topic', ['forum_post'], $taxonomy_args); 它前面有一个 ]; ,然后写在 ]; 前面即可
// 注册forum_tag分类法时设置rewrite参数
'rewrite' => array(
'slug' => 'forum_tag',
'with_front' => false,
'hierarchical' => false
),
// 找到 register_taxonomy('forum_tag', ['forum_post'], $taxonomy_args); 它前面有一个 ]; ,然后写在 ]; 前面即可效果验证
代码改完保存之后,去WordPress后台 -> 设置 -> 固定链接中点一下保存(无需修改任何东西),然后返回前端即可看到帖子标签和话题的链接变成了ID.html的形式。
把自己改好的复制了一份出来,下载后解压出来,把 class.init.php文件放在 网站根目录/wp-content/themes/zibll/inc/functions/bbs/inc/ 里面,直接执行替换即可(记得备份),然后再去WordPress后台 -> 设置 -> 固定链接中点一下保存。
















暂无评论内容