admin管理员组

文章数量:1124817

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

<?php
//
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

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

Share Improve this question asked Sep 15, 2023 at 20:29 RobRob 1
Add a comment  | 

1 Answer 1

Reset to default 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.

本文标签: categoriesExclude category in widget through child theme