0

I am trying to create a new sidebar. For that I have used the following code:

In functions.php

if ( function_exists('register_sidebar') )
register_sidebar();

if ( function_exists ('register_sidebar')) {
register_sidebar ('sidebar-custom');
} 

Then I created a file called sidebar-custom.php.The code for that file is as follows

<ul class=”sidebar”>
     <?php if ( !function_exists('dynamic_sidebar') ||
           !dynamic_sidebar('sidebar-custom') ) : ?>
     <?php endif; ?>
</ul>

But the sidebar area is not visible in the dashboard.I know I have done something terribly wrong. But dont know what it is. Can anyone help?

2 Answers 2

1

Register sidebar.

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

    if ( function_exists ('register_sidebar')) {
      register_sidebar( $args );
    }
}

Call the sidebar.

if ( is_active_sidebar('unique-sidebar-id') {
 dynamic_sidebar('unique-sidebar-id') );
}
0

Two problems. To register the sidebar you need to pass a minimum of the 'id'.

if ( function_exists ('register_sidebar')) {
  register_sidebar(array('id'=>'custom'));
}

Despite the Codex, I cannot get the sidebar to show up using only register_sidebar('custom');

That will make the sidebar show up on the backend. See the Codex for other parameters.

Secondly, there is nothing to load sidebar-custom.php. dynamic_sidebar('custom') would load a file named sidebar-custom.php, at least the Codex appears to suggest that, but since you have that function call inside sidebar-custom.php it never gets to run.

Third, you have curly quotes in you code. Look at the quotes around the class name.

So, to make this work put the following in functions.php (same as above):

if ( function_exists ('register_sidebar')) {
  register_sidebar(array('id'=>'custom'));
} 

Place the following in the template file where you want to the sidebar to load but not in sidebar-custom.php.

 if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('custom') ) :
 endif;

Your sidebar should now work, but again despite the Codex or possibly the Codex is just very confusing on the point, I can't get your sidebar-custom.php file to load.

To do that I have to explicitly use get_sidebar ('custom');

The more I worked on this, the more I came to believe that the Codex is poorly written when it come to custom sidebar files. The truth is in there but it is hard to see. Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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