2011-11-08 36 views
0

我正在使用此代碼將300個字符的限制設置爲the_content。設置the_content字符的限制

我如何設置當我想要使用它,當我不?

<?php 
add_filter("the_content", "plugin_myContentFilter"); 
function plugin_myContentFilter($content) 
{ 
// Take the existing content and return a subset of it 
return substr($content, 0, 300); 
} 
?> 
+0

在什麼條件下你想使用它,在什麼不是?無論如何,圍繞'add_filer()'的一個簡單的'if()'語句就可以做到。 –

+0

此代碼位於我的functions.php中,每當我使用the_content()時,它只顯示前300個字符。 但在一些地方,我想顯示the_content() – Osmar

+0

的整個內容我從最初的問題得到的。 **你的意思是什麼**「地點」 - 某些郵政類別,特定頁面等。 –

回答

0
<?php 

    function plugin_myContentFilter($content) { 
     if (!is_single()) { 
      return substr($content, 0, 300); 
     } else { 
      return $content; 
     } 
    } 
    add_filter("the_content", "plugin_myContentFilter"); 

?> 

會縮短內容超過300個字符,除非單後的網頁上。

+0

hm中顯示整個內容,但是我忘了說我想對pages.php做同樣的事情= /我試圖添加一個else if和did not work。 – Osmar

+0

@Osmar我不知道你的主題中有什麼樣的模板pages.php,但是如果你的意思是你不想在所有靜態頁面上縮短,請改變'if(!is_single()){到'if(!is_single()&&!is_page()){'。 –

+0

@Osmar如果您立即看到上述評論,請參閱編輯。到&& - 我想的不對。 –