WordPress.org

Ready to get started?Download WordPress

Forums

displaying different sidebar in posts and categories. (5 posts)

  1. David
    Member
    Posted 3 years ago #

    the site im working on is drjoelrosen.com although Im actually working on a localhost version of the site so I won't be able to show my work.

    What I am trying to do is display different sidebars for different categories but I want it to apply to the posts assigned to that cat as well.

    So far I have created different sidebars and category php files and that works great when on a category page but as soon as you click a post the default sidebar shows up again.

    I have been searching for a solution all day and keep coming up dry so I am hoping one of you kind folks can point me in the right direction!

  2. Use conditional tags.

    So, if you have a cat ID of 10, you could do:

    if(is_category(10) || in_category(10)) {
    // your sidebar stuff here
    }
  3. David
    Member
    Posted 3 years ago #

    where would I put that code though, thats kind of whats confusing me. Do I replace the get_sidebar portion with my conditional comments? would that work.

  4. You can do it in one of two ways.

    First, you can put the conditional statements inside of your sidebar.php, making it so part of the code in the sidebar.php is displayed for one condition, and another part for the other (else) condition.

    Second, you could replace get_sidebar() with the conditional statements and then create a second sidebar and call one of them for one condition, and the other for else condition. Like this:

    if(is_category(10) || in_category(10)) {
        get_sidebar('category'); // this gets the sidebar-category.php
    } else {
        get_sidebar(); // this gets the default sidebar.php
    }
  5. David
    Member
    Posted 3 years ago #

    as I was writing that it didn't work and about to give up I decided to try adding it to my category.php instead of my index.php and it worked for the category page but not the single post, so I added it to my single.php and now all is working perfect, thanks a bunch.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.