WordPress.org

Ready to get started?Download WordPress

Forums

Widget Logic - Widget shows on category and subcategory (5 posts)

  1. sirenetta1
    Member
    Posted 5 years ago #

    Hello all,

    I have multiple categories on my site. With widget logic, I'm able to show the widget on the category and single posts within that category, BUT if the category has a subcategory, when I click on that, the widget no longer shows.

    For example:

    MAIN CATEGORY: Dogs
    - Food post (under "Dogs")
    - Adopting post (under "Dogs")
    SUBCATEGORY: Poodle
    - Training post (under Dogs/Poodle)
    - Health post (under Dogs/Poodle)

    Where my main category id=3, this widget logic code will make the widget appear for anything under the "Dogs" category

    is_category(3) || (is_single() && in_category(3))

    I can expand it to also include the subcategory, by just adding to the code

    is_category(3) || (is_single() && in_category(3)) || is_category(6) || (is_single() && in_category(6))

    but is there a better way to do it? I'd like it to be automatic, so that when I'm adding a new subcategory, the widget automatically shows up because it is under category 3 (Dogs).

    Thanks for your help!

  2. musicmasteria
    Member
    Posted 5 years ago #

    I'm trying to do something like this as well and haven't found an answer yet...

    Mine is a little different though, it is:

    is_single() && (in_category(1) || in_category(2) || ...)

    category 1,2,3... all being under the parent category of '10' or whatever (just making up numbers).

  3. alanft
    Member
    Posted 5 years ago #

    see your other post on this.

  4. sirenetta1
    Member
    Posted 5 years ago #

    What other post? Can you provide a link? Thanks! :-)

  5. alanft
    Member
    Posted 5 years ago #

    SORRY! i tought this was a duplicate of musicmasteria's thread!

    http://wordpress.org/support/topic/253564?replies=10

    where i recommend he used the function at http://codex.wordpress.org/Template_Tags/in_category#Testing_if_a_post_is_in_a_descendant_category

    hope this helps

    A

Topic Closed

This topic has been closed to new replies.

About this Topic