• derlynad

    (@derlynad)


    Hello!

    I’m making preparations to migrate from the old theme to Twenty Twenty and found another issue.

    I created a new child theme, and tried to put a typical function to functions.php file in order to exclude some categories from the loop.

    /*Delete category from the loop*/
    
    function exclude_category($query) {
     if ($query->is_home){
     $query->set('cat','-37');} 
     return $query; }
    add_filter('pre_get_posts','exclude_category');

    This function worked just fine on my previous themes, but in the case of Twenty Twenty it returns a blank white screen and nothing more (in the front and admin panel).

    Maybe there is an alternative way to exclude categories from the loop, especially if I try to use a static home page, and the main loop with posts on the subpage (ex. example.com/blog)?

    Any ideas appreciated.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter derlynad

    (@derlynad)

    Any other similar functions like this:

    function filter_pre_get_posts( $query ) {
        if ( is_home() && $query->is_main_query() ) {
            $query->set( 'category__not_in', array( '37' ) );
        }
    }
    add_action( 'pre_get_post', 'filter_pre_get_posts' );

    result in the same blank screen ??

    Jarret

    (@jarretc)

    I tried the first block of code that you supplied adjusting for the category ID in my TwentyTwenty child theme and it removed the posts from the homepage loop.

    Maybe you have a typo somewhere in there causing the white screen? I’m not sure if you’re developing this on a live site or something like localhost but you should be able to check the error log somewhere or try setting WP_DEBUG in your wp-config.php to true to get the actual error message to display.

    Thread Starter derlynad

    (@derlynad)

    Thanks a lot, @jarretc! There was a typo indeed in another function above.

    Will this filter solution work for the subpage, as https://example.com/blog?

    Jarret

    (@jarretc)

    If /blog is the page you have set under Settings->Reading as the Posts Page then yes.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Adding exclude category function breaks the Theme’ is closed to new replies.