Title: Code for Custom Sidebars
Last modified: August 20, 2016

---

# Code for Custom Sidebars

 *  [HopefulGJL](https://wordpress.org/support/users/hopefulgjl/)
 * (@hopefulgjl)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/code-for-custom-sidebars/)
 * Hello,
 * I am looking to customize by sidebars, and am wondering if there is a fairly 
   easy way to do so with code. I don’t know all that much about code, but I have
   customized other codes on my site with good results.
 * I am aware there is a custom sidebars plugin, but I’m trying to not depend upon
   plugins too much. And, I’m not sure if it has a way for me to do what I am looking
   to do or not either.
 * There are certain pieces of my sidebar that I am looking to have displayed on
   all of the pages of my site. This happens automatically by default. However, 
   I am also looking for other pieces of my sidebar to appear only on specific pages.
   Basically, I’m looking to have things that appear on every page and things that
   appear only on certain pages there simultaneously.
 * Does anyone know how this can be accomplished? Is there a code I can add to my
   sidebar CSS? Or, is there more to it than that? Thanks in advance for your help!

Viewing 15 replies - 1 through 15 (of 19 total)

1 [2](https://wordpress.org/support/topic/code-for-custom-sidebars/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/code-for-custom-sidebars/page/2/?output_format=md)

 *  [Frumph](https://wordpress.org/support/users/frumph/)
 * (@frumph)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/code-for-custom-sidebars/#post-2817632)
 * Well it kind of depends on if you are hardcoding the widgets into your sidebar
   or not.
 * If you are, you can use conditional statements like is_home() is_page() is_single
   etc to wrap around it, if (is_home()) { }
 * [http://codex.wordpress.org/Conditional_Tags](http://codex.wordpress.org/Conditional_Tags)
 * If you are using widgets, there are “widget logic” plugins that let you choose
   locations and pages that you want individual widgets (that are dragged to the
   sidebar locations)
 * Example plugin:
    [http://wordpress.org/extend/plugins/widget-logic-visual/](http://wordpress.org/extend/plugins/widget-logic-visual/)
 * ^ there’s a bunch on the repo
 *  [marquex](https://wordpress.org/support/users/marquex/)
 * (@marquex)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/code-for-custom-sidebars/#post-2817701)
 * Why don’t you try custom sidebars
 * [http://wordpress.org/extend/plugins/custom-sidebars/](http://wordpress.org/extend/plugins/custom-sidebars/)
 * It’s really easy to use and not coding needed.
 *  [Frumph](https://wordpress.org/support/users/frumph/)
 * (@frumph)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/code-for-custom-sidebars/#post-2817702)
 * “I am aware there is a custom sidebars plugin, but I’m trying to not depend upon
   plugins too much. “
 * from original OP.
 *  Thread Starter [HopefulGJL](https://wordpress.org/support/users/hopefulgjl/)
 * (@hopefulgjl)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/code-for-custom-sidebars/#post-2817833)
 * Thank-you for the information. I have now tried to use the code from the page
   you provided a link to. However, I think that one reason it may not be working
   is because it says that there needs to be something specific done if there is
   a theme involved. I do have a theme.
 * The widget-logic visual plugin might be what I end up going with, but I do want
   to try and find the correct code if possible. That plugin looks perfect if I 
   cannot get the code right. Thank-you for that.
 * What I know works, at least to put something on all of my pages, is to enter 
   whatever I want my code to be in a widget.
 * In my sidebar page in the editor, I currently have the following code. It was
   there originally. It’s just one part of the overall code that is there. Is there
   something I can put after this code to indicate I want “a” on only page “a” and“
   b” on only page “b” and “c” on every page, etc?
 *     ```
       <div id="sidebar-right">
            <?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar('Sidebar_Right') ) : else : ?>
       ```
   
 *  [Frumph](https://wordpress.org/support/users/frumph/)
 * (@frumph)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/code-for-custom-sidebars/#post-2817834)
 * You can add the conditional directly to the if statement. Example:
 *     ```
       <?php if (!is_page('34') && dynamic_sidebar('Sidebar_Right') ) : else : ?>
       ```
   
 * This says if it’s NOT page ID # 34 and there’s something in the dynamic sidebar
   labelled sidebar_right then echo the contents of what is in that sidebar, else
   do what comes after :
 * So what you want to do is find out what the ID numbers of the pages you don’t
   want the sidebar in and do if !is_page(ID#) for them. (read that conditionals
   page for better examples)
 *  Thread Starter [HopefulGJL](https://wordpress.org/support/users/hopefulgjl/)
 * (@hopefulgjl)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/code-for-custom-sidebars/#post-2817835)
 * Thank-you again. I can see this is making progress.
 * This had interesting results. I put the page id# in on a page I wanted to test
   this with. I then put one code in the widget, and the other code after the : 
   else : part of the above code we last discussed.
 * The code I put in the sidebar somehow appeared twice on every page (one above,
   one below) except for the page that I put in the page id# for. On that page, 
   the code I put in after the : else : was there on only that page (hurray!), but
   then the code that I put in the widget still appeared under that.
 * Is there possibly code to enter after the : else : that would tell it to exclude
   this page on page id on only page id 1,2,3 etc?
    For example, if there was code
   to exclude pages, then whatever code I have would appear everywhere except for
   what I specifically told it to exclude.
 * Is there such a thing?
 *  [Frumph](https://wordpress.org/support/users/frumph/)
 * (@frumph)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/code-for-custom-sidebars/#post-2817836)
 * The “else” part means that whatever is in the else will be displayed if there
   is not anything in the sidebar ;/
 * and yeah you do the same thing if (is_page(‘##’)) { then do this; }
 *  Thread Starter [HopefulGJL](https://wordpress.org/support/users/hopefulgjl/)
 * (@hopefulgjl)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/code-for-custom-sidebars/#post-2817838)
 * Viola!
 * I think I have some code that will work. I won’t say it’s the most efficient,
   but it looks like a combination of what you provided along with something else
   I discovered is what will work.
 * In my theme functions editor, I found some code that where I was able to create
   a new sidebar where I can put the widgets in to. I found:
 *     ```
       ));
   
       if ( function_exists('register_sidebar') )
       	register_sidebar(array(
       		'name' => 'Sidebar_right',
       		'before_widget' => '<li id="%1$s" class="widget %2$s">',
       		'after_widget' => '</li>',
       		'before_title' => '<h2>',
       		'after_title' => '</h2>',
       	));
       ```
   
 * I just copied that code one row down, and changed ‘Sidebar_right’ to a new name,‘
   Sidebar_Right_2.
 * Then, I went back in to my sidebars editor, and applied the code that you provided
   as a test (changing ‘Sidebar_right’ to what I named the new one to, ‘Sidebar_Right_2).
 * This looks like it will work. I’ll just keep adding new sidebar places to drag
   widgets in to, and I think it’ll work.
    What I’m finding that works so far is
   to enter the following in to my sidebar editor:
 *     ```
       <div id="sidebar-center">
       	<?php if (!is_page('1') && dynamic_sidebar('Sidebar_Right_2') ) : else : ?>
   
           <?php endif; ?>
         </div>
         <div id="sidebar-center">
       	<?php if (!is_page('2') && dynamic_sidebar('Sidebar_Right') ) : else : ?>
   
         <?php endif; ?>
         </div>
       ```
   
 * After I add the code in to the sidebar editor area, I can then drag widgets around.
 * Now, excluding pages will be a lot of work, but I’ll take it since it seems to
   be the solution! Thank-you very much!
 * Would anyone happen to have any ideas as to why I’m getting a black dot next 
   to what I add to the sidebar? Could it be that there’s a list code somewhere 
   that’s causing that?
 *  [Frumph](https://wordpress.org/support/users/frumph/)
 * (@frumph)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/code-for-custom-sidebars/#post-2817839)
 * You can’t use two ID= ‘s of the same name in html, but otherwise 😉 .. you can
   make them classes or differ the two.
 *  Thread Starter [HopefulGJL](https://wordpress.org/support/users/hopefulgjl/)
 * (@hopefulgjl)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/code-for-custom-sidebars/#post-2817840)
 * Oh, thank-you for catching that. I think I might know what you mean.
 * If I take the first `(!is_page('1')` and change it to `(!is_page('1,2')` while
   leaving the second one as `(!is_page('2')`, it’s not excluding the first one 
   from page id#2.
 * It feels like I’m getting really close to the answer, though.
 * How do I make the classes you mentioned?
 *  [Frumph](https://wordpress.org/support/users/frumph/)
 * (@frumph)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/code-for-custom-sidebars/#post-2817841)
 * `<div class="sidebar-center">`
 * Remember, read the line on what you wrote as if it was english.
 * If (not) page # 2 and there’s something in dynamic_sidebar(‘whatever’) then display
   whats in the dynamic sidebar ‘whatever’ ELSE display whats in the else statement
   after the if statement.
 * So when you put if (not) page #1 AND #2 and there’s something in dynamic sidebar…
   etc..
 * What you wrote above, *should* exclude page ID #2 from both of them ;/ ..
 *  [Frumph](https://wordpress.org/support/users/frumph/)
 * (@frumph)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/code-for-custom-sidebars/#post-2817842)
 * Maybe you should write it differently..
 *     ```
       if (!is_page('1,2')) {
                 if (dynamic_sidebar('whatever')) : else :
                      // content if nothing is in the sidebar 'whatever';
                 endif;
       }
       ```
   
 *  [Digital Raindrops](https://wordpress.org/support/users/adeptris/)
 * (@adeptris)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/code-for-custom-sidebars/#post-2817843)
 * Widget logic if you want to switch off single sidebar widgets.
 * Another Way:
    Create your new sidebars in functions.php as per your code above.
 * Your sidebar code should be in a file called sidebar.php, if not then copy it
   to a file sidebar.php
 * Open sidebar.php and save as sidebar-{slug}.php so this could be sidebar-home.
   php, sidebar-one.php, sidebar-single.php, sidebar-archive.php, sidebar-search.
   php, sidebar-members.php, sidebar-foo.php, sidebar-bar.php, sidebar-foobar.php,
   anything you want after the hyphon ‘-‘ all lowecase single word.
 * Inside the copied file just change the dynamic_sidebar({$slug-name}) for your
   new sidebar.
 * Then in the templates files call the different sidebars with or without a conditional
   statement.
 * Example page.php
 *     ```
       <?php if (is_page('1,2')) : ?>
          <?php get_sidebar('one'); ?>
       <?php else : ?>
          <?php get_sidebar(); ?>
       <?php endif; ?>
       ```
   
 * Example single.php
 *     ```
       <?php get_sidebar('single'); ?>
       ```
   
 * Example search.php
 *     ```
       <?php get_sidebar('search'); ?>
       ```
   
 * Example archive.php
 *     ```
       <?php get_sidebar('archive'); ?>
       ```
   
 * Example index.php
 *     ```
       <?php if ( is_user_logged_in() ) : ?>
          <?php get_sidebar('members'); ?>
       <?php else : ?>
          <?php get_sidebar(); ?>
       <?php endif; ?>
       ```
   
 * HTH
 * David
 *  Thread Starter [HopefulGJL](https://wordpress.org/support/users/hopefulgjl/)
 * (@hopefulgjl)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/code-for-custom-sidebars/#post-2817844)
 * Thanks for providing more ideas.
 * I tried the class part in to what I had, and it didn’t seem to make a difference.
 * Also, if I put in page id#’s 1 and 2 for both lines of the code in what I had
   above, then things get excluded only on page id 1, and nothing on page id 2.
   
   This seems so close, but I’ll need to be able to constantly repeat a lot of the
   same page id’s to exclude them to use this, and it’s not allowing me to do so.
 * I did try various things with the different code you provided, but I for whatever
   reason am unable to get it to work. I’m not sure what I’m missing.
 * And, I think the bullet points I mentioned above are coming from that ‘li’ code,
   so I’m not sure what could be altered there to eliminate those bullet points 
   either.
 * David: I’ll take a look in to what you’ve provided information on too, thanks!
 *  Thread Starter [HopefulGJL](https://wordpress.org/support/users/hopefulgjl/)
 * (@hopefulgjl)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/code-for-custom-sidebars/#post-2817880)
 * David: Thank-you so very, very much! That is what is working.
 * So that anyone else reading this can hopefully understand what I did, I offer
   the below explanation:
 * I logged in to my hosting company’s CPanel, used the file manager, drilled down
   to my theme’s .php files, and copied + added new .php files as explained above
   by David.
 * Then, I created the new sidebars I wanted in my functions.php file with the following
   code (like this example):
 *     ```
       if ( function_exists('register_sidebar') )
       	register_sidebar(array(
       		'name' => 'Sidebar_2',
       		'before_widget' => '<id="%1$s" class="widget %2$s">',
       		'after_widget' => '<br />',
       		'before_title' => '<h2>',
       		'after_title' => '</h2>',
       	));
       ```
   
 * Please note this code is a bit different than what I posted above. I removed 
   the ‘li’ before the id, and for the after widget part, I replaced the ‘/li’ part
   with the br part so that my sidebar pieces will no longer have bullet points 
   next to them and each piece will have a space between.
 * Next, I went to my new sidebar.php file that I created in my CPanel file manager,
   and changed the code to:
    `<?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar('
   Sidebar_2') ) : else : ?>`
 * (The Sidebar_2 is what changed here.)
 * After that, I needed to go to my page.php file, and add the following:
 *     ```
       <?php if (is_page('2')) : ?>
         	<?php get_sidebar('sidebar2'); ?>
         <?php else : ?>
          	<?php get_sidebar(); ?>
         <?php endif; ?>
       <?php if (is_page('3')) : ?>
         	<?php get_sidebar('sidebar3'); ?>
         <?php else : ?>
          	<?php get_sidebar(); ?>
         <?php endif; ?>
       </div>
       <?php get_footer(); ?>
       ```
   
 * Please note that the code is repeated there with the different page id numbers,
   and that the different sidebar.php files are being called in each.
    From what
   I have seen so far with tests is that it seems to be saying that what I want 
   on page id 3 is contained in sidebar3, etc.
 * After this, I inputted my widgets in to the appropriate sidebar, and it all seems
   to be working just as I’m looking for so far.
 * I hope that makes sense to anyone else who is also trying to figure this out.
   Thanks everyone!

Viewing 15 replies - 1 through 15 (of 19 total)

1 [2](https://wordpress.org/support/topic/code-for-custom-sidebars/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/code-for-custom-sidebars/page/2/?output_format=md)

The topic ‘Code for Custom Sidebars’ is closed to new replies.

 * 19 replies
 * 4 participants
 * Last reply from: [HopefulGJL](https://wordpress.org/support/users/hopefulgjl/)
 * Last activity: [13 years, 9 months ago](https://wordpress.org/support/topic/code-for-custom-sidebars/page/2/#post-2817932)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
