5

I am very well aware of how to make multiple sidebars. But I believe my way is not proper way of adding multiple sidebars.

This is how I add multiple sidebars

If I simply wants to create a sidebar then I use sidebar.php file. BUT if I want to use another sidebar then I have to create another php file like sidebar-new.php. Then call this file as

 <?php 
      get_sidebar('new'); 
 ?>

That mean if I want to create 4 sidebar then I have to make 4 php files!

BUT I have seen many themes (In wordpress market) that provide many sidebars but they contains only one php file for sidebar (sidebar.php)! How do they do that? I have learned about making sidebars from google earlier but in search I only get the results that I am using right now (create multiple files for multiple sidebars).

So how can I create multiple sidebar without making Multiple php files!!???

  • I would look at creating a sidebar OOP class and then rather call the class instead of get_sidebar. This way, you can pass different values to the class as needed. I cannot really see any other solution without getting really messy – Pieter Goosen Jul 1 '16 at 7:15
6

Defining new sidebar with in your functions.php

<?php

if ( function_exists('register_sidebar') ) {

   register_sidebar(array(
   'before_widget' => '<li id="%1$s" class="widget %2$s">',
   'after_widget' => '</li>',
   'before_title' => '<h2 class="widgettitle">',
   'after_title' => '</h2>'
   ));

}?>

Once these are functions are defined, you will notice the extra sidebar appear in the WordPress Dashboard under the Appearance > Widgets option. It’s here that you can drag and drop all your widgets into your various sidebars.

<?php

if ( function_exists('register_sidebar') ) {

   register_sidebar(array(
   'name' => 'sidebar 1',
   'before_widget' => '<div id="%1$s" class="widget %2$s">',
   'after_widget' => '</div>',
   'before_title' => '<h2>',
   'after_title' => '</h2>'
    ));

   register_sidebar(array(
   'name' => 'footer sidebar 1',
   'before_widget' => '<div id="%1$s" class="widget %2$s">',
   'after_widget' => '</div>',
   'before_title' => '<h2>',
   'after_title' => '</h2>'
   ));

}?>

Adding new sidebar to your template

Within your sidebar.php file, change the call to your existing sidebar to include its name that you defined within the functions.php file earlier.

<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('sidebar 1') ) : ?>

   <h2>Articles by month</h2>
   <ul>
      <?php wp_get_archives('title_li=&type=monthly'); ?>
   </ul>
   <h2>Categories</h2>
   <ul>
      <?php wp_list_categories('show_count=0&title_li='); ?>
   </ul>

<?php endif; ?>

To add your new sidebar, you can either copy the above code or you can simply copy the following lines. Add these lines to wherever you’d like your new widgets to appear. In this example you can see from the name that I’m placing mine in the footer of my website. As before, don’t forget to specify the correct sidebar name. In the above code, the html that appears between the php statements is what will appear when there are no widgets added to your sidebar. This ‘default’ code can obviously be modified to suit your theme. In the following code, since there is no extra html, nothing will be displayed unless a widget has been added into the sidebar within your WordPress dashboard.

<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('footer sidebar 1') ) : ?>
<?php endif; ?>
  • ok I m little confused here! First, why u didnt use id in registering the sidebar And how do I call sidebar in my homepage? like this get_sidebar();! – Rishabh Jul 1 '16 at 9:59
  • ID is not use because calling sidebar through widget area and you can call on home page by simple calling name of widget area at your homepage. – Owais Alam Jul 1 '16 at 10:02
  • But why I cant see any sidebar (that just registered) in apearance -> widget ? – Rishabh Jul 1 '16 at 10:07
  • Did you apply above code? There is multiple way to add sidebar in your theme file. – Owais Alam Jul 1 '16 at 10:10
  • First code I did write in functions.php and second code I typed in functions.php (and later in home.php as well) and the last code I typed in sidebar.php. What else I need to do and where? – Rishabh Jul 1 '16 at 10:16
3

You can differentiate between sidebars inside sidebar.php. I don't know what your purpose is, but let's suppose you want different sidebars on single posts/pages and other pages.

In your functions.php you would register two sidebars with id's singular and default in the usual way with register_sidebar.

Then in sidebar.php you would include something like

if (is_singular) {dynamic_sidebar('singular');}
else {dynamic_sidebar('default');}

Of course, there are many ways to vary on this. You can display different sidebars depending on category or maybe no sidebar at all on a special page template called 'one-column'.

  • I have tried ur solution and result comes out as per my expectations. It only showing 1 sidebar! What I actually wanted to know a way of adding multiple sidebars on one page and in that page on the location of my choice. – Rishabh Jul 1 '16 at 8:01
  • You mean you want multiple calls to get_sidebar from index.php so you have sidebars in different places? – cjbj Jul 1 '16 at 8:06
  • Yes bcos If I want to show 2 diff sidebars one on footer and other on any other location i.e. header OR both sidebars at footer on single page, then how would i do that? – Rishabh Jul 1 '16 at 8:09
  • Right. This means you need to pass a piece of information from index.php (the place of the call) to sidebar.php. This is a tricky issue with mulitple solutions. You can read more about it here: wordpress.stackexchange.com/questions/228223/… – cjbj Jul 1 '16 at 8:18
0

You can create multiple sidebar using vc or wordpress editor by https://codecanyon.net/item/custom-sidebar-visual-editor-wordpress-plugin/17329853

  • Thanks for your suggestion, but I wanted to do it by myself. I mean without help of a plugin. But your answer might be helpful for future viewers :) – Rishabh Sep 1 '17 at 5:53

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.