問題描述
我是 WordPress 的新手,只是安裝了 3.3.1 版本。
我做了一些關於這個問題的搜尋,並找到一些答案,但它們與 2.7 版本相關,是 2-3 歲。
基本上,wp_title 功能在除了主頁之外的每個頁面都可以正常工作,它返回空白,我沒有任何標題。我可以只是硬編碼的標題,但我寧願不這樣做。
有罪行程式碼:
<title><?php wp_title ( '| So Fresh n' So Clean', true,'right' ); ?></title>
我在 3.3.1 中找不到有關此問題的任何事情,顯然我做錯了事情。
最佳解決方案
這是我從 Codex 讀取的內容:
If you are using a custom homepage with custom loops and stuff, you will have an empty
wp_title. Here goes a neat hack to add the description/tagline at thewp_titleplace on homepage:
<title><?php bloginfo('name'); ?> | <?php is_front_page() ? bloginfo('description') : wp_title(''); ?></title>
所以使用 is_front_page()在主頁上獲得標題,就是上面程式碼中的建議。
次佳解決方案
但是如果你使用一個靜態主頁,這是程式碼:
<title><?php bloginfo('name'); ?> » <?php is_front_page() ? bloginfo('description') : wp_title(''); ?></title>
第三種解決方案
更新
WordPress 4.4 棄用了 wp_title() 功能…
function some_name(){
add_theme_support( 'title-tag' );
}
add_action( 'after_setup_theme', some_name' );
在 functions.php 中執行此操作,並從頭刪除’title’ 標籤
第四種方案
從 Amna 的答案出發,我想出了以下程式碼,當有一個時候應該顯示頁面標題,然後是站點名稱。
<?php wp_title(' - ',TRUE,'right'); bloginfo('name'); ?>
帖子/頁面輸出:「頁面標題 – 站點名稱」
主頁輸出:”Site Name”
顯然,這也可以交換以顯示站點名稱。
<?php bloginfo('name'); wp_title(' - '); ?>
帖子/頁面輸出:「站點名稱 – 頁面標題」
主頁輸出:”Site Name”
X- 20045 X- 200 X- 200 200 -40 -40 200 200 -40 200 200 X- 200 200:200 的 X-
<?php bloginfo('name'); echo ' - '; is_front_page() ? bloginfo('description') : wp_title(''); ?>
帖子/頁面輸出:「站點名稱 – 頁面標題」
主頁輸出:「站點名稱 – 站點描述」
第五種方案
對於谷歌搜尋在 wordpress wp_title 空這是第一個結果。所以我以為我可以分享最優雅的解決方案。在 functions.php 中為 wp_title 新增一個過濾器。
function custom_wp_title( $title, $sep ) {
if ( is_feed() ) {
return $title;
}
global $page, $paged;
// Add the blog name
$title .= get_bloginfo( 'name', 'display' );
// Add the blog description for the home/front page.
$site_description = get_bloginfo( 'description', 'display' );
if ( $site_description && ( is_home() || is_front_page() ) ) {
$title .= " $sep $site_description";
}
// Add a page number if necessary:
if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
$title .= " $sep " . sprintf( __( 'Page %s', '_s' ), max( $paged, $page ) );
}
return $title;
}
add_filter( 'wp_title', 'custom_wp_title', 10, 2 );
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。