【问题标题】:WordPress: Is it possible to add a widget to footer/sidebar on widget activation?WordPress:是否可以在小部件激活时将小部件添加到页脚/侧边栏?
【发布时间】:2020-06-17 15:11:43
【问题描述】:

我已设法按照fuxia's solution 向页脚侧边栏添加默认小部件,但小部件似乎没有“激活”(我必须将其删除并手动将其添加回页脚侧边栏才能使小部件工作,使预设的小部件代码毫无意义)。我发现上述解决方案添加的小部件和从可用小部件列表中提取的相同小部件是不同的。下图显示了小部件页脚列表中小部件项目的不同 href 值。 (灰色框只是小部件名称)

我不确定这是否是问题背后的原因,但有没有办法将小部件添加到页脚侧边栏并让它工作/“激活”?

编辑:当前实现

add_action( 'widgets_init', function(){
    register_widget( 'MyWidget' );

    $sidebars = array ('a' => 'sidebar-1' );
    foreach ( $sidebars as $sidebar ) {
      register_sidebar(array (
      'name' => 'Footer',
      'id' => $sidebar,
      'before_title' => '<h2 class="widget-title">',
      'after_title' => '</h2>'
      ));
    }

    $active_widgets = get_option( 'sidebars_widgets' );
    if ( ! empty ( $active_widgets[ $sidebars['a'] ] )){ 
    return; }
    $counter = 1;
    $active_widgets[$sidebars['a']][0] = 'my_widget-'.$counter;
    update_option('sidebars_widgets',$active_widgets);
    dynamic_sidebar('sidebar-1');
}

【问题讨论】:

    标签: wordpress widget footer sidebar


    【解决方案1】:

    你能用下面的代码检查一下吗?

    函数。 php

    add_action( 'widgets_init', 'custom_sidebars' );
    
    /*Custom widgets*/
    function aavishkaar_custom_sidebars(){
    
    register_sidebar(
          array (
               'name'          =>  __( 'Footerwidget', 'Aavishkaar' ),
               'id'            =>  'footer-widget1',
               'description'   =>  __( 'Footer widget', 'Aavishkaar' ),
               'before_widget' =>  '<div class="col-md-2 col-3 footer-pd_0 widget_ul">',
               'after_widget'  =>  "</div>",
    
    
        )
    );
    
    }
    
    Then add menu to this and call it by  <?php dynamic_sidebar('footer-widget1');?>
    

    【讨论】:

    • 您好,您可以查看我问题的编辑部分吗?它似乎与您的代码内联,但我仍然面临 href 差异问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-26
    • 1970-01-01
    • 1970-01-01
    • 2013-01-16
    • 2017-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多