1

I have registered a sidebar with the following code :

function reg_l_sid(){
     $args = array(
     'name'          => __( 'left-sidebar', 'Tutorial-Blog' ),
     'id'            => 'left-sidebar',
     'description'   => '',
     'class'         => '',
     'before_widget' => '<li id="%1$s" class="widget %2$s">',
     'after_widget'  => '</li>',
     'before_title'  => '<h2 class="widgettitle">',
     'after_title'   => '</h2>' );

register_sidebar( $args );
}
add_action ('widgets_init','reg_l_sid');

and the sidebar is registered, then i added some widgets. then i tried to use the conditional is_active_sidebar to display a message if there are no widgets like this :

if ( is_active_sidebar( 'left-sidebar' ) ) {
     dynamic_sidebar( 'left-sidebar' );
} else {
    echo 'Please add widgets';
}

but it keeps showing the message "Please add widgets", while as i mentioned that there are widgets have been added. So where is the problem?

Update

To be mentioned that it works fine without the conditional.

  • 1
    Didn't you have added 'id' parameter inside $args? – Sonali Mar 1 '17 at 8:32
  • tried but no change – Mohamed Omar Mar 1 '17 at 8:35
3

Please try this one,because without id we i don't think that will work:

  function reg_l_sid()
  {
   $args = array( 'id' => 'sidebar-footer-6','name'=> 'Left-sidebar');
   register_sidebar($args);
  }
  add_action('widgets_init', 'reg_l_sid');

and inside template:

  if (is_active_sidebar('sidebar-footer-6')) {
        dynamic_sidebar('sidebar-footer-6');
   } else {
        echo 'Please add widgets';
   }
| improve this answer | |
  • it will work without id as core will provide an auto generated id from the name, but it will be sanitized version of the name which is likely to be different from the name, in this case probably ending in `left-sidebar (notice the lower "l"). – Mark Kaplun Mar 1 '17 at 8:38
  • thanks, but it didn't work, and i think no need to use id because wordpress states that i can use the name with is_active_sidebar – Mohamed Omar Mar 1 '17 at 8:42
  • I tried with only name in wordpress 4.7 version but didn't register sidebar with only name. – Sonali Mar 1 '17 at 8:43
  • No it worked with me and if i removed the condition every thing works fine with widgets visible inside the blog – Mohamed Omar Mar 1 '17 at 8:47
  • 2
    @MohamedOmar I guess the WordPress docs aren't very precise about using the sidebar name in is_active_sidebar(). This is only possible if this name has been used in a sanitized form as the id when registering. In fact if you check their source code, you can see that is_active_sidebar() only searches by id and register_sidebar() uses 'sidebar-##' as default if no id is supplied. – JHoffmann Mar 1 '17 at 9:08
1

Actually the problem has been solved but i don't know why this happened.. so after using the default WordPress function every thing worked fine, then i manged to delete the args i don't need ,returning back to using only the name argument ...and this was the starting point for me.. after checking every thing worked .. so my be it was a syntax problem but finally no problem with using the name argument only. Now i have this and it works just fine :

function reg_l_sid(){
   $args = array('name'=> __( 'Left-sidebar', 'Tutorial-Blog' ));
   register_sidebar( $args );
}
add_action ('widgets_init','reg_l_sid')

In sidebar :

if ( is_active_sidebar( 'Left-sidebar' ) ) {
     dynamic_sidebar( 'Left-sidebar' );
} else {
    echo 'Please add widgets';
}
| improve this answer | |
  • This is still not working on mine,but it's great that working on your theme.That's great. – Sonali Mar 1 '17 at 9:36
  • Yes may be it will not work on some, but we've known it has to do with id – Mohamed Omar Mar 1 '17 at 9:38
0

I have just put your code in my function file and also create a template to test your code. for me, it's working fine. Please re-evaluate your things, it might be working.

add_action( 'widgets_init', 'twentyseventeen_widgets_init' );
function reg_l_sid(){
     $args = array(
     'name'          => __( 'left-sidebar', 'Tutorial-Blog' ),
     'id'            => 'left-sidebar',
     'description'   => '',
     'class'         => '',
     'before_widget' => '<li id="%1$s" class="widget %2$s">',
     'after_widget'  => '</li>',
     'before_title'  => '<h2 class="widgettitle">',
     'after_title'   => '</h2>' );

register_sidebar( $args );
}

add_action ('widgets_init','reg_l_sid');

if ( is_active_sidebar( 'left-sidebar' ) ) {
     dynamic_sidebar( 'left-sidebar' );
} else {
    echo 'Please add widgets';
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.