WordPress:如何使用functions.php防止显示子类别的帖子?

时间:2019-05-16 13:27:11

标签: php wordpress

如何仅显示父类别下的帖子,如何隐藏子类别下的帖子?

谢谢!

1 个答案:

答案 0 :(得分:0)

将此代码放置在functions.php下

add_action('pre_get_posts', 'filter_out_children');

function filter_out_children( $query ) {
  if ( is_main_query() && is_category() && ! is_admin() ) {
     $qo = $query->get_queried_object();
     $tax_query = array(
       'taxonomy' => 'category',
       'field' => 'id',
       'terms' => $qo->term_id,
       'include_children' => false
     );
     $query->set( 'tax_query', array($tax_query) );
  }
}