WordPress.org

Ready to get started?Download WordPress

Forums

A little Help in Registering Sidebars (3 posts)

  1. sandeepbagchi
    Member
    Posted 2 years ago #

    I'm registering Sidebars for my theme in the following way.

    <?php add_action( 'widgets_init', 'bereishit_register_footer_sidebar' );?>
    <?php
    function bereishit_register_footer_sidebar() {
            register_sidebars(5,
                    array( 'name'   => 'Footer %d',
                    'id'            => 'footer-%d',
    		'before_widget' => '<div id="%1$s" class="widget %2$s">',
    		'after_widget'  => '</div>',
    		'before_title'  => '<h3 class="widget-title">',
    		'after_title'   => '</h3>',
    	));
    }
    ?>

    From footer.php am trying to display the sidebars

    <?php
    	/*
    	 *  Footer Sidebar
    	 */
    	get_sidebar( 'footer' );
    ?>

    sidebar-footer.php
    is as follows

    <?php
    /*-----------------------------------------------------------------------------------*/
    /* The template for displaying the footer sidebars.                                  */
    /*-----------------------------------------------------------------------------------*/
    ?>
    
    <?php
    	/* The footer widget area is triggered if any of the areas
    	 * have widgets. So let's check that first.
    	 *
    	 * If none of the sidebars have widgets, then let's bail early.
    	 */
    
           for ($i=1; $i<=5; $i++)
           {
               if (   ! is_active_sidebar( sprintf(__('footer-%d'), $i )))
               {
                   echo 'here I am returning';
                   echo sprintf(__('footer-%d'), $i );
                   return;
               }
           }
    
           // If we get this far, we have widgets. Let do this.
           echo 'here I am';
    ?>

    but not able to show the sidebar "is_active_sidebar" is failing for some reason. And the widget that i had added is disappearing :-(

  2. Brett
    Member
    Posted 2 years ago #

    That is one way to do it, but to register it why not just drop that code in your functions.php file? Like so:

    <?php
    
    if ( function_exists('register_sidebar') ) {
    	register_sidebar(array('name'=>'Sidebar',
    		'before_widget' => '<li id="%1$s" class="widget %2$s">',
    		'after_widget' => '</li>',
    		'before_title' => '<h2 class="widgettitle">',
    		'after_title' => '</h2>',
    	));
    	register_sidebar(array('name'=>'Footer',
    		'description' => __( 'Enough room for 4 widgets', 'leeu' ),
    		'before_widget' => '<div class="gs_3 widget fwidgets">',
    		'after_widget' => '</div>',
    		'before_title' => '<h4 class="widgettitle">',
    		'after_title' => '</h4>',
    	));
    
    }
    ?>
  3. sandeepbagchi
    Member
    Posted 2 years ago #

    It seems there register_sidebars isn't giving me the full control of the sidebar id.

    So, i had to add a function is functions.php to get better control while registering multiple sidebars.

    function bereishit_register_sidebars($number = 1, $args = array()) {
    
    	$number = (int) $number;
    
    	if ( is_string($args) )
    		parse_str($args, $args);
    
    	for ( $i=1; $i <= $number; $i++ ) {
    		$_args = $args;
    
    		if ( $number > 1 ) {
    			$_args['name'] = isset($args['name']) ? sprintf($args['name'], $i) : sprintf(__('Sidebar %d'), $i);
    		} else {
    			$_args['name'] = isset($args['name']) ? $args['name'] : __('Sidebar');
    		}
                    if ( $number >= 1 ) {
    			$_args['id'] = isset($args['id']) ? sprintf($args['id'], $i) : sprintf(__('Sidebar-%d'), $i);
    		}
    		register_sidebar($_args);
    	}
    }
    function bereishit_register_footer_sidebar() {
            bereishit_register_sidebars(5,
                    array( 'name'   => 'Footer %d',
                    'id'            => 'footer-%d',
    		'before_widget' => '<div id="%1$s" class="widget %2$s">',
    		'after_widget'  => '</div>',
    		'before_title'  => '<h3 class="widget-title">',
    		'after_title'   => '</h3>',
    	));
    }

    I feel its a bug in register_sidebars()

    As you can see i have taken the code for register_sidebars() and added few changes to give me better control over registered sidebars.

    Instead of modifying the wordpress code i choose to add this function in functions.php so that if in future register_sidebars() is fixed i can use it.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.