2014-10-26 181 views
0

我有我的WP功能的問題。我只需要它在博客文章上顯示。現在它會顯示在其他頁面上,例如FrontPage。WordPress的只顯示在文章

這是我在functions.php的功能:

function my_sp_function($content) { 
     $include = FALSE; 
     $post = FALSE; 

     if (in_category(array(3, 4, 5, 6, 91, 133))) { 

      $include = TRUE; 

     } 
     if (get_post_type(get_the_ID()) == 'post'){ 
      $post = TRUE; 
     } 

     if ($post = TRUE AND $include == TRUE) { 
       return $content; 
     } 
} 

任何人那裏知道我做錯了什麼?

我現在已經使用此代碼嘗試,它仍然顯示在頭版上的$內容..

function my_sp_function($content) { 
     $include = FALSE; 
     $post = FALSE; 

     if (in_category(array(3, 4, 5, 6, 91, 133))) { 

      $include = TRUE; 

     } 

     if(is_home() || is_front_page()) { 
     $include = FALSE; 
     } 

     if ($post == TRUE AND $include == TRUE) { 
       return $content; 
     } 
} 
+0

鉤子在哪裏?你必須以某種方式調用它......這將是關鍵。 – rnevius 2014-10-26 13:35:17

+0

你只需要echo my_sp_function();在single.php,index.php或其他帖子頁面上。你不應該在任何其他模板文件上調用它(如果用在header/footer/sidebar中,使用條件if(is_single()|| in_category($ cat)|| is_category($ cat)|| etc || etc)) 。 – ggdx 2014-10-26 13:39:07

+0

if($ post = TRUE ...'if to($ post == TRUE ...' – 2014-10-26 13:40:47

回答

0

如果你想只包括在主頁上的功能,你有兩個選擇 - 只將函數插入到主題中的single.php模板中,或者當is_single()爲true但is_page()爲false時,首選方法僅將您的函數掛接到the_content

function my_sp_function($content) { 
    $content .= "Some more stuff for the_content!"; 
    return $content; 
} 
if (is_single() && ! is_page()){ 
    add_filter('the_content', 'my_sp_function'); 
} 
相關問題