2017-08-04 63 views
0

我想用Wordpress插件覆盖页面的<title>标签。 我不想更改主题的代码。我只是想强制主题通过插件更改一些页面标题。使用Wordpress插件覆盖标题标签

主题使用add_theme_support('title-tag')。请注意,现在不推荐使用wp_title。

+0

https://codex.wordpress.org/Plugin_API/Filter_Reference/the_title – Scuzzy

回答

2

您的问题是,如果主题已经支持title-tag,则不能在主题中使用wp_title()。你的主题的<head>应该是这样的:

<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; 
} 

如果你这样做,它会很好地工作。

+0

链接不是很好的答案,除非你解释链接中的基础解决方案。 – Difster

+0

尝试使用更新的代码 –

+0

我删除了我的倒票。 – Difster