2012-12-31 63 views
3

我想添加一個函數到我的Wordpress主題,它會在每個帖子前後添加內容,但不會在任何頁面上添加內容。這似乎並不奏效,但不知道爲什麼。wordpress如果is_page功能不起作用

if(!is_page()) { 
    function custom_content($content) { 
    $content = 'before post content' . $content . 'after post content'; 
    return $content; 
    } 
    add_filter('the_content','custom_content'); 
} 

編輯:解我結束了該做的伎倆對我來說,用is_single只包括單帖。如果你想包括一個網頁上使用is_singular

function custom_content($content) { 
    if (is_single()) { 
     $content = 'before post content' . $content . 'after post content'; 
} 
    return $content; 
} 
add_filter ('the_content', 'custom_content', 0); 
+0

您是否測試過以確保您的if語句可以工作?試着把「回聲測試」這樣的東西放進去;「在if語句的開頭。 –

+3

'這個條件標籤檢查頁面是否正在顯示。這是一個布爾函數,意味着它返回TRUE或FALSE。這個標籤必須使用** BEFORE ** The Loop並且不能在The Loop內工作。https://codex.wordpress.org/Function_Reference/is_page –

+0

Ahh,darn it!感謝您指出了這一點。 – user1373779

回答