Title: Coding for multiple custom sidebars (elseif)
Last modified: August 19, 2016

---

# Coding for multiple custom sidebars (elseif)

 *  Resolved [Rich Owings](https://wordpress.org/support/users/gpsmapper/)
 * (@gpsmapper)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/coding-for-multiple-custom-sidebars-elseif/)
 * I have a custom sidebar called when a post (single.php) is from a specific category.
   It’s set up like this…
 *     ```
       <?php
       global $post;
       $side = false;
       foreach( (get_the_category($post->ID)) as $category) {
       if ($category->cat_ID == 168) {$side = true;};
       }
       if($side) {
       include(TEMPLATEPATH."/sidebar_custom.php");
        }
       else {
       include(TEMPLATEPATH."/sidebar.php");
        }
       ?>
       ```
   
 * Now I want to add another custom sidebar. I think I need an elseif statement,
   but I’m not quite sure of the syntax and where to insert it. Can anyone help?

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

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/coding-for-multiple-custom-sidebars-elseif/#post-1491416)
 * Assuming you want sidebar_custom123.php when the cat_ID is 123, this should work(
   UNTESTED!):
 *     ```
       <?php
       global $post;
       foreach( (get_the_category($post->ID)) as $category) {
          if ($category->cat_ID == 168) {
             include(TEMPLATEPATH."/sidebar_custom.php");
          } elseif ($category->cat_ID -- 123) {
             include(TEMPLATEPATH."/sidebar_custom123.php");
          } else {
             include(TEMPLATEPATH."/sidebar.php");
           }
       }
       ?>
       ```
   
 *  Thread Starter [Rich Owings](https://wordpress.org/support/users/gpsmapper/)
 * (@gpsmapper)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/coding-for-multiple-custom-sidebars-elseif/#post-1491450)
 * Thanks vtxyzzy. Oh so close. It called the right one, but then kept repeating
   the primary sidebar. They just stacked on top of each other. Any idea why?
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/coding-for-multiple-custom-sidebars-elseif/#post-1491489)
 * If the post is in multiple categories, that would happen. Or, if the code is 
   in the loop, it is getting the sidebar for each post. In that case, you will 
   need a counter. Try putting in a break like this:
 *     ```
       <?php
       global $post;
       foreach( (get_the_category($post->ID)) as $category) {
          if (++$sidebar_counter > 1) break;
          if ($category->cat_ID == 168) {
             include(TEMPLATEPATH."/sidebar_custom.php");
          } elseif ($category->cat_ID -- 123) {
             include(TEMPLATEPATH."/sidebar_custom123.php");
          } else {
             include(TEMPLATEPATH."/sidebar.php");
           }
       }
       ?>
       ```
   
 *  [Darrell Schauss](https://wordpress.org/support/users/darrellonsite/)
 * (@darrellonsite)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/coding-for-multiple-custom-sidebars-elseif/#post-1491528)
 * The numbers in array() are comma seperated list of category IDs you want the 
   sidebar to show up in.
 *     ```
       <?php
       $cat_id = get_query_var('cat');
   
       if(in_array($cat_id,array("10","11","12"))){
        //show sidebar code
       }
   
       if(in_array($cat_id,array("15","16","17"))){
        //show sidebar code
       }
       ?>
       ```
   
 *  Thread Starter [Rich Owings](https://wordpress.org/support/users/gpsmapper/)
 * (@gpsmapper)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/coding-for-multiple-custom-sidebars-elseif/#post-1491649)
 * Thanks vtxyzzy and darrellonsite. Family obligations will keep me from trying
   these until Sunday or Monday, but I’ll report back here once I do.
 *  Thread Starter [Rich Owings](https://wordpress.org/support/users/gpsmapper/)
 * (@gpsmapper)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/coding-for-multiple-custom-sidebars-elseif/#post-1491763)
 * [@vtxyzzy](https://wordpress.org/support/users/vtxyzzy/) –
 * Well, the second one (sidebar_custom123) shows up in the right place when I try
   that, but not the first one (sidebar_custom).
 * [@darrellonsite](https://wordpress.org/support/users/darrellonsite/) – I tried
   this approach, but it’s not working. It could well be some sort of syntax error
   on my part. Here’s what I did…
 *     ```
       <?php
       $cat_id = get_query_var('cat');
       if(in_array($cat_id,array("165","166"))){
             include(TEMPLATEPATH."/sidebar_custom.php");
          }
       if(in_array($cat_id,array("163","164"))){
             include(TEMPLATEPATH."/sidebar_custom163.php");
          } else {
             include(TEMPLATEPATH."/sidebar.php");
           }
       ?>
       ```
   
 * I also tried making the second if an elseif, but that didn’t seem to change things.
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/coding-for-multiple-custom-sidebars-elseif/#post-1491764)
 * I wonder if the problem is that the post you are testing is in more than one 
   category? If so, you would show the sidebar for the first category found, not
   necessarily the one you were looking for.
 * The code by darrellonsite should work (with your elseif change) if this for a
   category query, but not for other types.
 *  Thread Starter [Rich Owings](https://wordpress.org/support/users/gpsmapper/)
 * (@gpsmapper)
 * [16 years ago](https://wordpress.org/support/topic/coding-for-multiple-custom-sidebars-elseif/#post-1491769)
 * Yes, most of the posts have multiple categories, typically a parent and a child.
 * And just to be clear, I’m looking to display the custom sidebars when a single
   post is displayed from the appropriate category. What I’m attempting has nothing
   to do with category view.
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [16 years ago](https://wordpress.org/support/topic/coding-for-multiple-custom-sidebars-elseif/#post-1491770)
 * OK. Try this variation. It will loop through all the categories assigned to a
   post and include the sidebar for the first match it finds. If no matches are 
   found, it will include the default.
 *     ```
       <?php
       global $post;
       $counter = 0;
       foreach( (get_the_category($post->ID)) as $category) {
          if ($category->cat_ID == 168) {
             ++$counter;
             include(TEMPLATEPATH."/sidebar_custom.php");
          } elseif ($category->cat_ID -- 123) {
             ++$counter;
             include(TEMPLATEPATH."/sidebar_custom123.php");
          }
          if ($counter) break;
       }
       if (!$counter) {
          include(TEMPLATEPATH."/sidebar.php");
       }
       ?>
       ```
   
 *  Thread Starter [Rich Owings](https://wordpress.org/support/users/gpsmapper/)
 * (@gpsmapper)
 * [16 years ago](https://wordpress.org/support/topic/coding-for-multiple-custom-sidebars-elseif/#post-1491798)
 * That did it. Woohoo! Thank you so much for taking the time to help folks like
   me. I greatly appreciate it.
 * A comment and question, if I may…
 * I believe that the line
    `} elseif ($category->cat_ID -- 123) {` should be `}
   elseif ($category->cat_ID == 123) {`
 * Also, if I want to add a third custom sidebar, can I just add another elseif 
   statement?
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [16 years ago](https://wordpress.org/support/topic/coding-for-multiple-custom-sidebars-elseif/#post-1491807)
 * Betrayed by the flying fingers!! You are correct about the equal signs, and about
   adding another esleif.
 * If your problem is solved, please use the dropdown at top right to mark this 
   topic ‘Resolved’.
 * BTW, if you are using this in the loop, this simpler code might work:
 *     ```
       <?php
       if (in_category(168)) {
          include(TEMPLATEPATH."/sidebar_custom.php");
       } elseif (in_category(123)) {
          include(TEMPLATEPATH."/sidebar_custom123.php");
       } else {
          include(TEMPLATEPATH."/sidebar.php");
       }
       ```
   
 *  Thread Starter [Rich Owings](https://wordpress.org/support/users/gpsmapper/)
 * (@gpsmapper)
 * [16 years ago](https://wordpress.org/support/topic/coding-for-multiple-custom-sidebars-elseif/#post-1491835)
 * Thanks. I had to check, but it is indeed outside the loop. I’ve marked the topic
   resolved. Thanks again for your help.
 *  [stuartsmythe](https://wordpress.org/support/users/stuartsmythe/)
 * (@stuartsmythe)
 * [16 years ago](https://wordpress.org/support/topic/coding-for-multiple-custom-sidebars-elseif/#post-1491910)
 * Hi there this is great thanks.
 * If I wanted to call the sidebar_custom.php in category 168, 356, and call sidebar_custom123.
   php in catergory, 25 , 74
 * How would i do this??
 * Thanks
    Kindly
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [16 years ago](https://wordpress.org/support/topic/coding-for-multiple-custom-sidebars-elseif/#post-1491912)
 * Try this:
 *     ```
       <?php
       if (in_category(array(168,356))) {
          include(TEMPLATEPATH."/sidebar_custom.php");
       } elseif (in_category(array(25,74))) {
          include(TEMPLATEPATH."/sidebar_custom123.php");
       } else {
          include(TEMPLATEPATH."/sidebar.php");
       }
       ```
   
 *  Thread Starter [Rich Owings](https://wordpress.org/support/users/gpsmapper/)
 * (@gpsmapper)
 * [16 years ago](https://wordpress.org/support/topic/coding-for-multiple-custom-sidebars-elseif/#post-1491913)
 * You’ll also need to modify functions.php. Mine looks like this:
 * `register_sidebar(array('name'=>'Sidebar Custom','before_title'=>'<h4>','after_title'
   =>'</h4>'));`

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

The topic ‘Coding for multiple custom sidebars (elseif)’ is closed to new replies.

## Tags

 * [category](https://wordpress.org/support/topic-tag/category/)
 * [custom](https://wordpress.org/support/topic-tag/custom/)
 * [sidebar](https://wordpress.org/support/topic-tag/sidebar/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 15 replies
 * 4 participants
 * Last reply from: [Rich Owings](https://wordpress.org/support/users/gpsmapper/)
 * Last activity: [16 years ago](https://wordpress.org/support/topic/coding-for-multiple-custom-sidebars-elseif/#post-1491913)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
