我的jquery知识仍然几乎不存在,所以我不确定the solution @Howdy\\u McGee在评论中建议的作品。
无论如何,作为适当的参考,从链接
只需更换\'your_widget\' 在下面的代码中显示小部件的名称(两处)。
\'sortreceive\' 事件仅在小部件添加到侧栏时调用,而\'sortstop\' 每当您在侧边栏内移动小部件或将其移除时,都会调用它。
\'sortstop\' 在添加小部件时也会调用,但由于某些原因,ui。输入设置不正确,所以我使用了“sortreceive”来解决这个问题。
jQuery(\'div.widgets-sortables\').bind(\'sortstop\',function(event,ui){
var id = jQuery(ui.item).attr(\'id\');
if (id) {
var widget_type = id.match(/widget-[0-9]+_(.+)-[0-9]+/i)[1];
if (widget_type == \'your_widget\') {
// do stuff;
}
}
})
jQuery(\'div.widgets-sortables\').bind(\'sortreceive\',function(event,ui){
var id = jQuery(ui.item).attr(\'id\');
var widget_type = id.match(/widget-[0-9]+_(.+)-__i__/i)[1];
if (widget_type == \'your_widget\') {
// do stuff;
}
})
我最近在
an answer 其中,可以从特定侧栏的小部件数组中删除特定的小部件。这里我们使用
sidebars_widgets 筛选以从所有侧栏中删除特定小部件,侧栏除外。
因此,简而言之,错误地添加到侧栏的小部件不会显示在前端,也不会返回trueis_active_sidebar() 检查该小部件是否是添加到特定侧栏的唯一小部件。
您可以尝试以下代码,只需确保相应地更改小部件和侧栏值。
add_filter( \'sidebars_widgets\', function ( $sidebars_widgets )
{
// Return our filter when we are on admin screen
if ( is_admin() )
return $sidebars_widgets;
/**
* Widget we need to target. This should be the name/id we used to register it
*
* EXAMPLE
* parent::__construct(
\'widget_category_posts\',
_x( \'Category Posts Widget\', \'Category Posts Widget\' ),
[ \'description\' => __( \'Display a list of posts from a selected category.\' ) ]
);
*
*/
$custom_widget = \'widget_category_posts\';
// The sidebar ID we need to run the widget in
$sidebar_accept = \'sidebar-2\';
// We have come this far, let us wrap this up
// See if our custom content widget exists is any sidebar, if so, get the array index
foreach ( $sidebars_widgets as $sidebars_key=>$sidebars_widget ) {
// Skip the wp_inactive_widgets set, we do not need them
if ( $sidebars_key == \'wp_inactive_widgets\' )
continue;
// Only continue our operation if $sidebars_widget are not an empty array
if ( $sidebars_widget ) {
foreach ( $sidebars_widget as $k=>$v ) {
/**
* Look for our custom widget, if found, unset it from the $sidebars_widgets array
* @see stripos()
*/
if ( stripos( $v, $custom_widget ) !== false ) {
// If we are on a single page and the sidebar is $sidebar_accept, do not unset
if ( is_single() && $sidebars_key == $sidebar_accept )
continue;
unset( $sidebars_widgets[$sidebars_key][$k] );
}
} // endforeach $sidebars_widget
} // endif $sidebars_widget
} // endforeach $sidebars_widgets
return $sidebars_widgets;
});
总之,这只是一个PHP解决方案,它只适用于前端,但我建议您仍然要寻找一个合适的jquery解决方案,其中小部件只绑定到后端的特定侧栏。正如我所说,链接中的jquery解决方案未经测试,我不知道是否真的有效