問題描述

我是 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 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。