我知道还有其他类似问题,但找不到可靠的答案。所以:
首先激活事物(简化代码):
add_action( 'after_setup_theme', 'theme_setup' );
function theme_setup() {
add_theme_support('title-tag');
}
第二,从header.php中删除标题标签。
第三,在页面模板上,在调用get_header()
之前,添加如下内容:
add_filter('wp_title', 'set_custom_title', 10, 3);
function set_custom_title($title, $sep, $seplocation){
return 'test';
}
嗯,在任何模板(页面,档案,自定义分类法或帖子类型档案)中,这根本不起作用。没什么。 WordPress本身就是在生成标题。
为什么?难道我做错了什么?请注意,这段代码曾经可以使用:在其他网站/主题中使用。
可能是wp5.2.0的问题吗?
答案 0 :(得分:1)
对于仍然存在wp_title
过滤器问题的任何人,我建议添加更高的优先级值。较高的优先级值将确保您的过滤器已执行,并且不会被主题或安装的插件中的其他过滤器覆盖。请参见以下内容:(参考号:https://developer.wordpress.org/reference/functions/add_filter/)
// the 9999999 priority value will force this filter to be executed closer to the end. A lower number corresponds with earlier execution
add_filter('wp_title', 'set_custom_title', 9999999, 3);
function set_custom_title($title, $sep, $seplocation){
return 'test';
}
答案 1 :(得分:0)
因此,感谢@Vel,答案是重新添加了标题标签(即使在以前的wp版本中,也不知道直到您要删除它的哪个版本才是抬头)。
我当前的工作代码:
//functions.php
add_action( 'after_setup_theme', 'theme_setup' );
function theme_setup() {
add_theme_support('title-tag');
}
//header.php
<title><?php wp_title('|', true, 'right'); ?> | <?php echo get_bloginfo('name') ?></title>
//page templates
$window_title = // do something
add_filter('wp_title', function($title, $sep, $seplocation) use($window_title){ return $window_title; }, 10, 3);
答案 2 :(得分:0)
尝试使用以下代码-
add_filter('document_title_parts', function($titles){
return array('title' => 'Custom Title');
});