WordPress.org

Ready to get started?Download WordPress

Forums

Custom sidebar category per page (11 posts)

  1. chowpay
    Member
    Posted 6 years ago #

    Figured out how to create a custom side bar per page to exclude categories I didnt want to display on the page. The problem is now that I have the sidebar the way I like on the page when I click on a post in the side bar, the post appears (which is awesome) but the side bar is now back to stock and displaying all the categories.

    For example I have a page called BOARDS and in the side bar it has posts of surf boards, did this by excluding all other categories in the page template, calling sidebar2.php. But now I click on one of the posts and it'll revert back to the same side bar displaying all the categories.. help?

    Thanks

  2. chowpay
    Member
    Posted 6 years ago #

    bump

  3. shua
    Member
    Posted 6 years ago #

    You are navigating from page.php template to single.php template (or so it sounds). Create a conditional in single.php that if the post is within the boards category show the correct sidebar content.

    does that help?

  4. chowpay
    Member
    Posted 6 years ago #

    Ah that make sense, the problem was I just don't know which file controls the posts. But yes thats exactly what I need to do.

    Do you have more detail on how this is done? because I can tell single.php to use the sidebar2.php but I dont know where to go to tell posts within single.php to use sidebar2.php.. does that make sense?

    Thanks

  5. chowpay
    Member
    Posted 6 years ago #

    actually its looks like its navigating to archive.php

    So I added this :<?php if (is_page(7)) {include ('sidebar2.php'); }
    else}
    else {include ('sidebar.php'); }
    ?>

    But I dont think the archive.php knows what page im on so instead it going straight to the sidebar.php instead of sidebar2.php

  6. chowpay
    Member
    Posted 6 years ago #

    AH hA!

    Got it so on the posts use custom sidebars:
    In case anyone needs this.. So its easy enough to make a custom sidebar appear depending on the page you are on. But the posts on the side bar of those pages will still link out to the original side bar because the data is being pulled from archive.php , or single that php.. Below is an example on how to exclude the category for archive.php but i'm sure it's the same for anything else.

    1. this will remove the "Archive" text depending on the category oyu sepecify

    <?php /* If this is a category archive for category n */ if (is_category(n)) { ?>
    <h3><?php single_cat_title(); ?></h3>
    <?php /* If this is a category archive*/ }elseif (is_category()){?><h3>Archive for the ‘<?php single_cat_title(); ?>’ Category</h3>

    2. Most important this will define which side bar to use during your static content on the archives.php page.

    <?php if (is_category(n)) {include ('sidebar2.php'); }
    else if (is_category(2)) {include ('sidebar2.php'); }
    else {include ('sidebar.php'); }
    ?>

    Thanks everyone, if there is a better solution, which im sure there is feel free to let me know or continue this thread!

  7. shua
    Member
    Posted 6 years ago #

    good job!

  8. chowpay
    Member
    Posted 6 years ago #

    Run in to a little php problem How do I do an OR statment to exclude multiple categories?

    Ie <?php if (is_category(n)) {include ('sidebar2.php'); }
    else if (is_category(2)) {include ('sidebar2.php'); }
    else {include ('sidebar.php'); }
    ?>

    Lets say I want to exclude categories 1,2,3,4

    <?php if (is_category(1,2,3,4)) {include ('sidebar2.php'); }
    else if (is_category(2)) {include ('sidebar2.php'); }
    else {include ('sidebar.php'); }

    how do I tell it to do something like IF is_category =1 or 2 or 3 etc etc then use 'sidebar2.php?

    Thanks

  9. chowpay
    Member
    Posted 6 years ago #

    Eh figured it out

    <?php if (is_category(1||2||3||4)) {include ('sidebar2.php'); }..

    um I guess a better question would be is there a way to put an includes? so I dont have to type out that messy code.

    IE
    <?php if (is_category($excludeList) {include ('sidebar2.php'); }..

  10. chowpay
    Member
    Posted 6 years ago #

    Edit to above

    <?php if (is_category(1||2||3||4)) {include ('sidebar2.php'); }..

    needs to be <?php if (is_category(array(1,2,3,4)) {include ('sidebar2.php'); }..

  11. chowpay
    Member
    Posted 6 years ago #

    the page that needs to be touched is archive.php. I'll upload a better solution that filters by slugs rather than manually entering the category ID

Topic Closed

This topic has been closed to new replies.

About this Topic