• Resolved innuendo

    (@innuendo)


    Is there a function or procedure I can use to test if a given category is a child (subcategory) within the loop? Any helpful ideas are welcome. Thanks in advance.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Where would this be needed, a single post page or on a category query?

    Thread Starter innuendo

    (@innuendo)

    It is needed on the index page within The Loop.

    The way it is structured now, I am reading a group of categories from an array from within a for loop. I would then like to filter the categories within the for loop with an if statement (and perform an action on only the subcategories).

    I was hoping there would be a function that would return TRUE if a category was a child. Unfortunately, I haven’t been able to find one like that. Any ideas?

    Thread Starter innuendo

    (@innuendo)

    Nevermind, I think I found a way to do it.

    if ($this_category->category_parent == 0) {

    If this array you mention holds all info for a category from its entry in the categories table, you should be able to test on the category_parent record for that category. For example, using get_the_category() to collect the categories for a post, you can use something like:

    <?php
    $categories = get_the_category();
    foreach($categories as $category) :
    if($category->category_parent) :
    ?>
    ~DO THIS~
    <?php
    endif;
    endforeach;
    ?>

    If you need to test for a specific category parent:

    if('1' == $category->category_parent)

    '1' being the category ID of the parent category.

    EDIT: I rarely “nevermind.” ;)

    Thread Starter innuendo

    (@innuendo)

    Sorry about that, I just experimented with a few different things and found something that worked. Thanks for your time!

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Looking for a way to distinguish between categories and subcategories’ is closed to new replies.