2012-12-23 44 views
0

我無法獲得我的自定義WordPress主題中的標題以正確顯示。標題屬性設置如下:<title><?php wp_title('|', true, 'right'); ?></title>WordPress的HTML標題搞砸了

我直接從2012年的主題採取該代碼。當我激活2012年主題時,標題看起來很完美,但是當我使用上述相同代碼的自定義主題時,我會將網站的地址作爲主頁上的標題,並在其他每個頁面上獲取頁面名稱後面跟着一個「|」。任何想法可能會導致這一點?我找了第二個標題標籤,但迄今沒有。它毀了我的SEO。

將此添加到我的functions.php:

function twentytwelve_wp_title($title, $sep) { 
    global $paged, $page; 

    if (is_feed()) 
     return $title; 

    // Add the site name. 
    $title .= get_bloginfo('name'); 

    // Add the site description for the home/front page. 
    $site_description = get_bloginfo('description', 'display'); 
    if ($site_description && (is_home() || is_front_page())) 
     $title = "$title $sep $site_description"; 

    // Add a page number if necessary. 
    if ($paged >= 2 || $page >= 2) 
     $title = "$title $sep " . sprintf(__('Page %s', 'twentytwelve'), max($paged, $page)); 

    return $title; 
} 
add_filter('wp_title', 'twentytwelve_wp_title', 10, 2); 
+0

你閱讀整本頁面http: //codex.wordpress.org/Function_Reference/wp_title? – j08691

+1

如果您發佈屏幕截圖或鏈接到頁面(最好是後者),回答您的問題會更容易。另外,正如@ j08691所提到的,確保您仔細檢查WordPress Codex。他們在記錄CMS的每個功能方面做得非常出色。 – Jules

回答

1

的博客名稱缺失。這就是爲什麼只顯示頁面標題。嘗試這樣的事情有一個想法:

<title> 
    <?php 
    bloginfo('name'); 
    echo " | "; 
    if (is_home()) { 
     _e("Home"); 
    } 
    else { 
     // Get the custom Title for this page, if any. 
     if (defined('Title')) { 
     $LimitWords = Title; 
     echo string_limit_words($LimitWords, 4) . " ..."; 
     } 
     else { 
     // Get the default Title 
     $LimitWords = wp_title('', FALSE); 
     echo string_limit_words($LimitWords, 4) . " ..."; 
     } 
    } 
    ?> 
    </title> 

對於代碼工作,你必須在functions.php樣式表目錄中添加此功能:

if (!function_exists('string_limit_words')) { 
    function string_limit_words($string, $word_limit) { 
     $words = explode(' ', $string, ($word_limit + 1)); 
     if (count($words) > $word_limit) array_pop($words); 
     return implode(' ', $words); 
    } 
    } 
+0

你是什麼意思博客「名稱」缺失?切換到另一個主題時,會顯示博客名稱。那麼這是不是意味着博客名稱被設置? – EGHDK

+0

'<?php wp_title('|',true,'right'); ?>'這就是你的問題。在那裏,博客名稱丟失。 –

+0

但這是2012年主題的確切代碼。它在2012年的主題中運作良好。我明白你在說什麼,但現在我只是對2012版主題如何處理該代碼感到困惑,而我的主題不適用於此。 – EGHDK