簡體   English   中英

如何從博客文章中排除特定類別

[英]How to exclude a specific category from blog posts

如何從 WordPress 中的博客文章中排除特定類別但如果博客文章選擇了兩個類別,一個來自排除類別,另一個來自包含我想顯示博客文章。

function exclude_category_home( $query ) {
    if ( $query->is_home ) {
        $query->set( 'cat', '-7' );
    }
    return $query;
}
 
add_filter( 'pre_get_posts', 'exclude_category_home' );

我嘗試在 function.php 中使用這個 function,它隱藏了排除的類別,但是當為一篇博文選擇兩個類別時,它仍然隱藏

/* you may try this code and use category__not_in function */

   function exclude_posts_from_home_page( $query )
   {
       if( $query->is_home() )
       {
           $query->set( 'category__not_in', array( 7 ) ); // here array of all category ids
      }
   }
   add_action( 'pre_get_posts', 'exclude_posts_from_home_page' );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM