0

Hi I wanted to create extra sidebar in my theme and I am not sure if what I did is correct.

In my theme functions.php I found this function:

    function blankslate_widgets_init() {
register_sidebar( array (
'name' => __('Sidebar Widget Area', 'blankslate'),
'id' => 'primary-widget-area',
'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
'after_widget' => "</li>",
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
}

and Inside I pasted the new sidebar so it is like that now

register_sidebar( array (
'name' => __('Sidebar Widget Area', 'blankslate'),
'id' => 'primary-widget-area',
'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
'after_widget' => "</li>",
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );


register_sidebar( array (
'name' => __('Left Sidebar', 'blankslate'),
'id' => 'left_sidebar',
'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
'after_widget' => "</li>",
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
}

after I've done this the widget appears in the dashboard perfectly

then I add this code where I wanted the sidebar to appear

<?php dynamic_sidebar('left_sidebar'); ?>

and that worked perfectly for me.

I'm not sure if this is the correct way and just worried if my theme will crush.

Can anyone advise me please?

0

Your function seems fine and it's probably hooked on init :

add_action('init','blankslate_widgets_init')
function blankslate_widgets_init() {
 //code
}

But in your theme you probably want to add some checking :

<?php if (is_active_sidebar('left-sidebar')) dynamic_sidebar('left-sidebar'); ?>

I would use left-sidebar instead left_sidebar as sidebar ID perhaps.

  • While is does not seem to matter at all, the Widgets API uses 'widgets_init' to register sidebars. – Charles Clarkson Aug 9 '13 at 16:29

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.