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 11 Answer
Reset to default 0Try 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
版权声明:本文标题:categories - Exclude category in widget through child theme 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736646882a1946129.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论