Support » Themes and Templates » register_sidebar id not working

  • Resolved Opally

    (@opally)


    I’ve written a function to register 3 sidebars.
    If I don’t specify an ID => value, then the call to dynamic_sidebar(‘name’) works just fine.

    But if I specify an ID => value (all lowercase, no spaces, unique) then both dynamic_sidebar(‘name’) and dynamic_sidebar(‘id’) fail.

    But I’d really like to assign an ID to a widget.

    Here’s my code:
    in theme functions.php

    if ( function_exists('register_sidebar') ) {
    
        function osc_widgets_init() {
    
            register_sidebar(array(
                'name'=>'Sidebar for Blog List',
                'id' => 'sidebar_for_blog_list',
                'before_widget' => '<section class="widget">',
                'after_widget' => '</section>',
                'before_title' => '<h1>',
                'after_title' => '</h1>',
            ));
            register_sidebar(array(
                'name'=>'Sidebar for Pages',
                'id' => 'sidebar_for_pages',
                'before_widget' => '<section class="widget">',
                'after_widget' => '</section>',
                'before_title' => '<h1>',
                'after_title' => '</h1>',
            ));
            register_sidebar(array(
                'name'=>'Sidebar for Footer',
                'id' => 'twitter_hash_tag-3',
                'before_widget' => '<li class="widget">',
                'after_widget' => '</li>',
                'before_title' => '<h2 class="widgettitle">',
                'after_title' => '</h2>',
            ));
        }
        add_action( 'widgets_init', 'osc_widgets_init' );
    }

    As an example, in footer.php

    <?php if ( dynamic_sidebar('Sidebar for Footer') ) :
      else :
      ?>
      <?php endif; ?>

    That doesn’t work. Neither does this:

    <?php if ( dynamic_sidebar('twitter_hash_tag-3') ) :
      else :
      ?>
      <?php endif; ?>

    So, the only thing that works so far is letting WP assign default IDs and calling dynamic_sidebar(‘name’).

    Suggestions on what I’m doing wrong?

    [Moderator – Moved the themes and templates]

Viewing 2 replies - 1 through 2 (of 2 total)
  • I have the following code in a theme right now and it works perfectly fine:

    function blm_register_sidebars() {
    	register_sidebar( array(
    			'id' => 'primary',
    			'name' => __( 'Primary Sidebar', 'blm_basic' ),
    			'description' => __( 'The following widgets will appear in the main sidebar of your blog.', 'blm_basic' ),
    			'before_widget' => '<aside id="%1$s" class="widget %2$s">',
    			'after_widget' => '</aside>',
    			'before_title' => '<h2>',
    			'after_title' => '</h2>'
    	) );
    	register_sidebar( array(
    			'id' => 'secondary',
    			'name' => __( 'Secondary Sidebar', 'blm_basic' ),
    			'description' => __( 'The following widgets will appear in the main sidebar of your pages.', 'blm_basic' ),
    			'before_widget' => '<aside id="%1$s" class="widget %2$s">',
    			'after_widget' => '</aside>',
    			'before_title' => '<h2>',
    			'after_title' => '</h2>'
    	) );
    	register_sidebar( array(
    			'id' => 'footer1',
    			'name' => __( 'Footer Column 2', 'blm_basic' ),
    			'before_widget' => '<aside id="%1$s" class="widget %2$s">',
    			'after_widget' => '</aside>',
    			'before_title' => '<h3>',
    			'after_title' => '</h3>'
    	) );
    }

    In one of my template:

    <?php if ( ! dynamic_sidebar( 'secondary' ) ) : ?>
    
    	<?php endif; ?>

    etc…

    The only difference I can see it the use of ! in my template. Maybe try that?

    Thread Starter Opally

    (@opally)

    Thanks for the reply, Christine!!

    Well, what I discovered is that the WP documentation is not complete for ID specification.

    When I removed the underscores from the ID names, it works.

    Hyphens are fine; underscores, not so much.

    Someone with permission needs to update this page:
    http://codex.wordpress.org/Function_Reference/register_sidebar

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘register_sidebar id not working’ is closed to new replies.