• Is it possible to set-up a different sidebar template for each category on my blog?

    I’m not sure how to go about doing this? I’d like to setup different sidebars through the widgetized format instead of hard coding them into the php… But first I’d like to know if this is even possible to do and how?

    I found the single post php and the main page php that calls the sidebar so I see how I can edit those. Drilling down to the category though, I have no idea?

Viewing 10 replies - 1 through 10 (of 10 total)
  • You can use the is_category conditional tag to define what shows up on which category.

    How elegant you decide to make the solution is up to you and your coding skills (all in one sidebar.php, create multiple templates sidebar-categoryA.php, etc)

    Thread Starter sburkowski

    (@sburkowski)

    Thanks Anne!

    Yes, I’m thinking separate sidebar files since I’m not that good at coding to be honest. Novice skills. I’d even prefer to have separate widgetized sidebars if I could figure out how to set that up?

    My main question is. Where would I insert the is_category conditional tag? Would that go at the end of the single.php where it says:

    <?php get_sidebar(); ?>

    Are you trying to display a different sidebar for each single post based on the post that category belongs to? If so, and the post has multiple categories, which category do you want to use to determine the proper sidebar?

    Or are you trying to have a different sidebar for your each of your category archivies (Category Templates)?

    Thread Starter sburkowski

    (@sburkowski)

    This might make it easier to see what I’m trying to do.

    On this blog https://drivemileone.com there’s multiple dealership divisions… For example Hall, Heritage, MotorWorld, etc. When you click on those ‘categories’ it shows all the posts that have been categorized as Hall, Heritage, etc.

    I’d like to customize the sidebar that’s shown when inside each category… So for example I could have the facebook API or maybe links and ads specific to those categories (dealerships) showing up.

    There should only be 1 category selected for each post, but in the event there is multiple categories selected for a post I’d want to default to just the general sidebar being shown instead of one of the custom versions specific to the dealerships (like shown on the blog home page).

    I hope that makes sense?

    Going with annedorko’s suggestion, in your Category Templates you will use something like:

    <?php
    if (is_category()){
      $current_cat = intval( get_query_var('cat') );
      get_sidebar($current_cat);  //for category 76 get sidebar-76.php
    }
    ?>

    Thread Starter sburkowski

    (@sburkowski)

    First off, Michael and Anne THANKS!!! ?? I really appreciate the help and advice!

    Second, if I’m understanding correctly I would duplicate my sidebar.php file and rename the duplicate to something like sidebar-hall.php and customize that for the category ‘hall’.

    Then I would go into my page.php template or archive.php (I’m not sure which?) and change the following code:

    <?php include(TEMPLATEPATH."/sidebar.php");?>

    To this instead…

    <?php
    if (is_category()){
      $current_cat = intval( get_query_var('cat') );
      get_sidebar($current_cat);  //for category Hall get sidebar-hall.php
    }
    ?>

    Am I correct?

    Thread Starter sburkowski

    (@sburkowski)

    Slight correction to the above post…

    In my archive.php file the code for the sidebar is

    <?php get_sidebar(); ?>

    and in the page.php its

    <?php include(TEMPLATEPATH."/sidebar.php");?>

    But now that I think about it I’m guessing the archive.php is where the code change needs to happen? I don’t see anything referring to a ‘category template’ in my templates files within the theme editor?

    It might be better for you to copy your theme’s archive.php to category.php then use the suggested code in category.php.

    Related:
    Template Hierarchy

    Thread Starter sburkowski

    (@sburkowski)

    If I’m understanding correctly these are the steps?

    1) I create the category.php file by duplicating my archive.php file

    2) in the new category.php file I change the code <?php get_sidebar(); ?>

    to

    <?php
    if (is_category()){
      $current_cat = intval( get_query_var('cat') );
      get_sidebar($current_cat);  //for category Hall get sidebar-hall.php
    }
    ?>

    And that’s it? My theme will recognize the new category.php file?

    Yes that should be it.

    My theme will recognize the new category.php file?

    That’s not really a correct way to describe the process, as it is WordPress and the Template Hierarchy that will ‘decide’ to use category.php. Just being picky ??

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Different sidebar for each category?’ is closed to new replies.