使用 Wordpress 插件覆盖标题标签 [英] Overwrite title tag using a Wordpress plugin
本文介绍了使用 Wordpress 插件覆盖标题标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 Wordpress 插件覆盖页面的
标记.我不想更改主题的代码.我只是想通过插件强制主题更改一些页面标题.
I am trying to overwrite the <title>
tag of a page using a Wordpress plugin.
I don't want to change the theme's code. I just wanna force the theme to change some page titles via the plugin.
主题使用add_theme_support('title-tag')
.请注意,现在不推荐使用 wp_title.
The theme uses add_theme_support( 'title-tag' )
. Note that the use of wp_title is now deprecated.
推荐答案
你的问题是不能使用 wp_title()
如果主题已经支持title-tag
.主题的 应如下所示:
Your problem is that you can not use wp_title()
in the theme if the theme already supports title-tag
. The <head>
of your theme should look like this:
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<?php wp_head(); ?>
</head>
过滤器和title-tag
支持:
add_action( 'after_setup_theme', 'my_theme_functions' );
function my_theme_functions() {
add_theme_support( 'title-tag' );
}
add_filter( 'wp_title', 'custom_titles', 10, 2 );
function custom_titles( $title, $sep ) {
//set custom title here
$title = "Some other title" . $title;;
return $title;
}
如果你这样做,它将完美地工作.
If you do this, it will work perfectly.
这篇关于使用 Wordpress 插件覆盖标题标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文