2017-08-04 62 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