将小部件限制为特定的已注册侧栏

时间:2015-09-24 作者:DᴀʀᴛʜVᴀᴅᴇʀ

我有一个小部件,我想只允许与单曲一起使用。php提要栏。当我参考法典时register sidebar 没有办法限制数组中的小部件。我已经用“WordPress限制小部件发布”进行了研究,但它们只是我搜索结果中返回的插件。当我将搜索参数更改为page以查看是否可以获得返回的内容时,我引用的是:

  • Disable Specific Widgets on Selected Pages of a WordPress Website? 它已经有五年的历史了,建议使用插件

  • How to add a specific widget to only 1 page? 是一篇两年前的帖子loop_start 但我没有看到任何涉及loop_start 或者如何通过POST实现它。

    是否有一种方法可以将小部件定位为只允许idregister_sidebar? 我不想使用插件,我想学习如何正确编码它。

    编辑:

    根据评论,我有一个自定义小部件。这在我的单打中被称为。php组件<?php get_sidebar( \'foobar\' );?>. 当有人在管理中时,我想限制小部件可以应用到的位置:

    enter image description here

    因此,在上面的图像中,我希望只有以下选项Post Sidebar. 我可以在sidebar-foobar.php 但我正在努力学习如何更多地利用小部件。

2 个回复
最合适的回答,由SO网友:Pieter Goosen 整理而成

我的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解决方案未经测试,我不知道是否真的有效

SO网友:icetique

作为对脚本的补充,我编写了一个脚本,它从下拉列表中隐藏了侧栏(我在任何地方都找不到)。我对原来的小部件进行了一些反向工程。js Wordpress代码来写这个。

允许拖动的完整解决方案&;仅拖放到指定的侧栏并过滤下拉列表(只需将其放入准备好文档的jQuery管理脚本中):

function allowedSidebars(allowed)
{
    // this variable will have index of first visible sidebar
    var first = null;
    $(\'.widgets-chooser-sidebars li\').removeClass(\'widgets-chooser-selected\').each(function(index)
    {
        // the data(\'sidebarId\') is set up by wordpress, let\'s make us of it
        if(-1 === $.inArray($(this).data(\'sidebarId\'), allowed))
        {
            $(this).hide();
        }
        else if(first == null)
        {
            first = index;
        }
    });
    // choose first visible sidebar as default
    if(first != null)
    {
        $(\'.widgets-chooser-sidebars li\').eq(first).addClass(\'widgets-chooser-selected\');
    }
}
$(\'#available-widgets .widget .widget-title\').on(\'click.widgets-chooser\', function()
{
    var widget = $(this).closest(\'.widget\');
    // we want to run our script only on slideDown, not slideUp
    if(!widget.hasClass(\'widget-in-question\'))
    {
        // there is only one sidebar list per all widgets, so we have to show all the sidebars every time
        $(\'.widgets-chooser-sidebars li\').show();
        switch(widget.find(\'input[name="id_base"]\').val())
        {
            // your widgets here
            case \'your_widget_id\':
                // allowed sidebars for widget
                allowedSidebars([\'your-sidebar-id\', \'your-second-sidebar-id\']);
            break;
        }
    }
});
// this will make drag and drop working only for specified sidebars
$(\'.widget\').on(\'dragcreate dragstart\', function( event, ui ) {
    var id = $(this).find(\'input[name="id_base"]\').val();
    // probably you may want to change it to switch
    if(id == \'your_widget_id\')
    {
        $(this).draggable({
            connectToSortable: \'#your-sidebar-id, #your-second-sidebar-id\'
        });
    }
});

相关推荐

是否可以取消对特定帖子类型的POSTS_PER_PAGE限制?

我想知道我是否可以取消特定帖子类型的posts\\u per\\u页面限制。在存档中。php页面我显示不同的帖子类型,对于特定的“出版物”帖子类型,我想显示所有帖子。我如何在不影响传统“post”类型的情况下实现这一点?