怎么快速解决WordPress标题中横线-被转义的问题

Wordpress3年前 (2022)发布 SUYEONE
950 0 0

WordPress 中,你可能遇到一个情况,即页面标题中的破折号“-”在源代码中被转义为“-”。这可能影响到你的 seo,因为理想的标题分隔符应为“-”。以下是一些解决方案

1. **修改模板文件**:
找到 `header.php`,通常位于 `/wp-content/themes/your-theme/` 文件夹下。将 `wp_title(“|”)` 中的 `|` 替换为 `-`。然而,这样做后,源代码中的破折号可能仍被转义为 `–`。

2. **利用 wptexturize 钩子**:
可以通过禁用 wptexturize 过滤器来避免破折号被转义。在你的主题的 `functions.php` 文件中添加以下代码:
“`php
function disable_wptexturize_title() {
rEMOve_filter(‘the_title’, ‘wptexturize’);
remove_filter(‘wp_title’, ‘wptexturize’);
remove_filter(‘single_post_title’, ‘wptexturize’);
}
add_Action(‘init’, ‘disable_wptexturize_title’);
“`
这将阻止 wptexturize 对标题进行转义。

3. **自定义 wp_title 函数**:
使用以下代码替换 `wp_title`,并添加破折号:
“`php
echo trim(wp_title(”, false, ‘right’)) . ‘-‘;
bloginfo(‘name’);
“`

4. **使用 HTML 实体解码**:
创建一个新函数来解码标题中的实体,并应用到过滤器中:
“`php
function htML_entity_decode_title($title) {
$title = str_replace(“-“, “-“, $title);
$title = html_entity_decode($title);
return $title;
}
add_filter(‘the_title’, ‘html_entity_decode_title’);
add_filter(‘wp_title’, ‘html_entity_decode_title’);
“`
将这段代码添加到 `functions.php` 文件。

选择上述任一方法可以解决标题中破折号被转义的问题,确保你的页面标题在源代码中和页面上都显示正确。推荐使用方法三,因为它直接禁用了导致问题的过滤器,而不会引入额外的编码或解码步骤。

© 版权声明

相关文章

暂无评论

暂无评论...
☺一键登录开启个人书签等功能!