WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Cutomizing or Removing the sidebar depending on wordpress categories? (7 posts)

  1. shafee85
    Member
    Posted 4 years ago #

    Hi,

    I am creating a website where there are multiple amounts and levels of categories. I would want to have the ability to customize the sidebar (either by not displaying the sidebar in certain categories, or changing its content depending on there categories)

    So my questions are,
    1. How do i not display the sidebar depending on a certain category.
    2. How do i change content of the sidebar depending on a certain category.

    Would really appreciate any help as i am not very strong with PHP. i know that for the 2nd question i will need to create a file called sidebar2.php. but how do i link that to the main files?

    Thanks for reading.

  2. MichaelH
    Member
    Posted 4 years ago #

    You first need to understand the Template Hierarchy and Category Templates.

    If you create a Category Template for your specific categories, then in the category template, say for category ID 2, you could just use:

    get_sidebar('cat2'); // will include sidebar-cat2.php
  3. shafee85
    Member
    Posted 4 years ago #

    Thank you very much for the reply. The links helped me understand the workings around themes quite a bit better.

    The code you quoted works perfectly if it is for a sigle category. I applied it to multiple categories but it doesn't work correctly.

    <?php if (is_category('7'||'8'))
    	get_sidebar('cat7');
    else
    	get_sidebar();
    	?>

    Yes the code applies to category 7 and 8, but it also applies to other categories as well.

    What am i doing wrong? Im pretty weak in coding unfortunately.

  4. shafee85
    Member
    Posted 4 years ago #

    Ok i found out the error of my above code. The code should read as follows,

    <?php
    if (is_category('7') || is_category('8'))
    get_sidebar('cat7');
    else
    get_sidebar();
    ?>

    Thanks MichaelH! you rock!

    I've got another question which is related to this thread. Is there anyway i can give the parent category id and make that automatically apply to all the child categories underneath it?

    Taking the above example into account, if i apply the above if condition to category id 7, is there a way to apply the same if condition to all the children of category id 7?

    This will enable the code to be more dynamic and help me to stop changing the category id each time i need to change them.

    Hope there is a solution for this.

  5. MichaelH
    Member
    Posted 4 years ago #

    From some note:

    <?php
    // in category template, test if queried category is child descendant of another category
    if ( is_category() ) {
    $parent = 7;
    $categories = get_categories('include='.get_query_var('cat'));
    //echo "<pre>"; print_r($categories); echo "</pre>";
    if ( $categories[0]->category_parent == $parent ) {
    echo 'category ' . $categories[0]->name . ' is a child of category ' . $parent;
    }
    }
    ?>
  6. shafee85
    Member
    Posted 4 years ago #

    Thanks for the help again. :)

  7. gauchomatt
    Member
    Posted 4 years ago #

    I am trying to do this as well, but I cannot seem to implement this into my theme?!
    http://www.lift22.com/clients/scholl/?cat=8

    I would like this page to not have a sidebar as well as everything else in the loop. Any assistance please!? Thank you!!

Topic Closed

This topic has been closed to new replies.

About this Topic