WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Coding for multiple custom sidebars (elseif) (16 posts)

  1. Rich Owings
    Member
    Posted 4 years ago #

    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?

  2. vtxyzzy
    Member
    Posted 4 years ago #

    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");
        }
    }
    ?>
  3. Rich Owings
    Member
    Posted 4 years ago #

    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?

  4. vtxyzzy
    Member
    Posted 4 years ago #

    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");
        }
    }
    ?>
  5. Darrell Schauss
    Member
    Posted 4 years ago #

    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
    }
    ?>
  6. Rich Owings
    Member
    Posted 4 years ago #

    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.

  7. Rich Owings
    Member
    Posted 4 years ago #

    @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 - 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.

  8. vtxyzzy
    Member
    Posted 4 years ago #

    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.

  9. Rich Owings
    Member
    Posted 4 years ago #

    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.

  10. vtxyzzy
    Member
    Posted 4 years ago #

    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");
    }
    ?>
  11. Rich Owings
    Member
    Posted 4 years ago #

    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?

  12. vtxyzzy
    Member
    Posted 4 years ago #

    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");
    }
  13. Rich Owings
    Member
    Posted 4 years ago #

    Thanks. I had to check, but it is indeed outside the loop. I've marked the topic resolved. Thanks again for your help.

  14. stuartsmythe
    Member
    Posted 4 years ago #

    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

  15. vtxyzzy
    Member
    Posted 4 years ago #

    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");
    }
  16. Rich Owings
    Member
    Posted 4 years ago #

    You'll also need to modify functions.php. Mine looks like this:

    register_sidebar(array('name'=>'Sidebar Custom','before_title'=>'<h4>','after_title'=>'</h4>'));

Topic Closed

This topic has been closed to new replies.

About this Topic