如何从WordPress博客帖子页面中排除类别

时间:2017-01-19 02:26:57

标签: php wordpress loops

我的头版使用静态页面,博客帖子使用另一页。

如何排除某些类别显示在我的博客页面上。我已经看过头版的代码,但无法让它适用于我的博客页面

function exclude_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '-4,-5,-6,-7,-8,-10,-12,-13' );
}
}
add_action( 'pre_get_posts', 'exclude_category' );

2 个答案:

答案 0 :(得分:1)

add_action( 'pre_get_posts', 'exclude_category_posts' );

function exclude_category_posts( $query ) {
if( $query->is_main_query() && $query->is_home() ) {
$query->set( 'cat', '-359, -2' );
    }
}

通过输入你的functions.php

来试试这个

答案 1 :(得分:0)

奇怪的是你的代码应该以这种方式运行。

function exclude_category($query)
{
    if ($query->is_home() && $query->is_main_query())
    {
        //Alternative method
        $query->set('category__not_in', array(4, 5, 6, 7, 8, 10, 12, 13));
    }
}

add_action('pre_get_posts', 'exclude_category');

希望这有帮助!

相关问题