• Is it possible to easily create a new sidebar for different categories or pages? I would like to add an About Me section and a Journalism section to my blog, but I would like these sections to have different information in the sidebar than the main page (my blog is at http://www.pdberger.com if it helps). Many thanks to anyone who can help.

Viewing 12 replies - 1 through 12 (of 12 total)
  • If by “pages” you mean the Pages feature in WP – the answer is yes, you can have different sidebar for certain Pages. There might be an easier way, but I’d create a new page-template and a mysidebar.php which would be called only by that template. When it’s done write your Pages for that section or whatever using the new template.

    For posts in categories you’d have to wait until a PHP guru comes around and tells you how to go with that… but in the meantime there is a good reading in Codex: Category Templates

    I would do this using if-else PHP statements in the sidebar.php file. In fact, I already do this using if-else statements. I combine them with the is_ functions to test what page is being loaded. So for your situation, you might add this to sidebar.php:

    <?php
    if ( is_page('About Me') ) {
    ?>
    special about me sidebar stuff
    <?php
    }
    elseif ( is_category('Journalism') ) {
    ?>
    special journalism sidebar stuff
    <?php
    }
    ?>

    If you’re using the default theme, there is already a whole bunch of if-else statements in the sidebar.php, so you’ll have to merge this with those. Find the line:

    <?php /* If this is a category archive */ if (is_category()) { ?>

    and change the if to elseif and delete the <?php. Then paste what I had above in above that line, but leave out the last ?>. That should work.

    Yeah… much nicer.
    – probably I should learn some PHP 🙁

    Thread Starter pdberger

    (@pdberger)

    Sounds like a great solution. Thank you Moshu and Avenir for your help. pdberger

    Is it also possible to display the child categories for a specific category in the way explained above?

    Let’s say I have categories English and German. If I’m on the English category page I only want to have the child-categories of the English-category listed plus a specific English menue.

    You can also emply the same if…else concept in the page template, and have several completely different sidebars, rather than within the sidebar file. Probably a matter of how different the sidebar’s are going to be.

    Thanks, sounds good. As I’m a beginner, how can I make only one specific parent category (and their sub/children categories) to be displayed?

    see Avenir’s post, but instead of the using (‘About Me’), simply use the cat #.

    Using the method laid out above I can get my sidebar to change for the parent, but it reverts for children. Any ideas?

    I believe as it now is you would have to include the children in the if statement. Might be a nice addition to have an is_page_or_children()

    Excuse my ignorance of php syntax. Is there a way to do this in the one line? eg. I’m using
    if ($cat == “4” )
    how do I add other options
    if ($cat in(“4”, “5”)) ? or something

    Or do I need to include the same code for all possible options
    if ($cat == “4” ){?>
    Blah!
    <?php } elseif {($cat == “5” ){?>
    Blah!
    <?php } else {{?>
    Whatever!

    The previous posts were about Pages and their children Pages (aka sub-Pages) and Pages do not have categories.
    So, obviously your problem/question is not related because in your code you have “categories”.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘New Sidebar for Each Category’ is closed to new replies.