問題描述

我是 Wordpress 的新手。我需要檢查標頭檔案,如果當前頁面是部落格頁面?

我檢查過,但我找不到方法。幫我吧

最佳解決方案

如果透過’blog page’,您將在靜態頁面設定為閱讀設定中的帖子頁面,那麼可以透過這樣做來檢查它:

if ( is_front_page() && is_home() ) {
  // Default homepage
} elseif ( is_front_page() ) {
  // static homepage
} elseif ( is_home() ) {
  // blog page
} else {
  //everyting else
}

When you use is_home() and is_front_page(), you have to use them in the right order to avoid bugs and to test every user configuration.

(來源:條件標籤 – 部落格頁)

或簡單地:

if ( !is_front_page() && is_home() ) {
  // blog page
}

或者更簡單 (我想):

if ( is_home() ) {
  // blog page
}

次佳解決方案

您可以在主題 functions.php 檔案中使用以下內容:

function is_blog () {
    return ( is_archive() || is_author() || is_category() || is_home() || is_single() || is_tag()) && 'post' == get_post_type();
}

然後把它放在你正在檢查的檔案中:

<?php if (is_blog()) { echo 'You are on a blog page'; } ?>

你可以在你的 functions.php 檔案中使用 Hook 來掛鉤上面的內容,使它出現在每個頁面上。

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。