好了,我终于明白了!如果不看看Widget Logic
它是这样的:首先注册一个要过滤的函数sidebar_widgets
:
add_filter( \'sidebars_widgets\', \'my_filter_widgets\', 10);
然后循环遍历所有小部件,跳过您不关心的小部件,如果您不满足您关心的小部件的条件,请删除它们,如下所示:
function my_filter_widgets($sidebars_widgets)
{
foreach($sidebars_widgets as $widget_area => $widget_list)
{
foreach($widget_list as $pos => $widget_id)
{
// We\'re only after widgets named my_custom_widget, this can vary and you will need to find out, try doing a var_dump() on $sidebars_widgets
if(substr($widget_id, 0, 17) != \'my_custom_widget\') {
continue;
}
// Those that have not been skipped over, check your condition, and if it doesn\'t meet the condition, remove it from the $sidebars_widgets array
if (\'press_articles\' != get_post_type() || !is_single()) {
unset($sidebars_widgets[$widget_area][$pos]);
}
}
}
return $sidebars_widgets;
}