0

I desperately try to exclude categories through following snippet in my functions.php of my child theme:

<?php
//https://developer.wordpress.org/themes/advanced-topics/child-themes/#how-to-create-a-child-theme
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function my_theme_enqueue_styles() {
    $parenthandle = 'twentyseventeen-style'; // This is 'twentyfifteen-style' for the Twenty Fifteen theme.
    $theme        = wp_get_theme();
    wp_enqueue_style( $parenthandle,
        get_template_directory_uri() . '/style.css',
        array(),  // If the parent theme code has a dependency, copy it to here.
        $theme->parent()->get( 'Version' )
    );
    wp_enqueue_style( 'child-style',
        get_stylesheet_uri(),
        array( $parenthandle ),
        $theme->get( 'Version' ) // This only works if you have Version defined in the style header.
    );
}
add_action( 'pre_get_posts', 'wpdocs_exclude_categories' );

function wpdocs_exclude_categories( $query ) {
    echo "foo";
    $query->set( 'cat', '-10' );
}

The $query->set command seems to be processed but it does not have any effect on my category widget. :-(

Any ideas how to fix that?

Thx, Rob

1 Answer 1

0

Try using WordPress' built-in filter for widget display called widget_categories_args. You can add a filter function to your child theme's functions.php that modifies the arguments passed to the wp_list_categories() function, which is what the Categories widget uses (if that's what the widget you're trying to modify) to pull its list of categories. Here's an example:

function filter_widget_categories_args($args) {
    $args['exclude'] = array(10); // ID of the category to exclude
    return $args;
}
add_filter('widget_categories_args', 'filter_widget_categories_args');

The function filter_widget_categories_args will modify the original $args array by adding an exclude key.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.