Forums

[resolved] Looking for a way to distinguish between categories and subcategories (6 posts)

  1. Innuendo
    Member
    Posted 3 years ago #

    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.

  2. Kafkaesqui
    Moderator
    Posted 3 years ago #

    Where would this be needed, a single post page or on a category query?

  3. Innuendo
    Member
    Posted 3 years ago #

    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?

  4. Innuendo
    Member
    Posted 3 years ago #

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

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

  5. Kafkaesqui
    Moderator
    Posted 3 years ago #

    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." ;)

  6. Innuendo
    Member
    Posted 3 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic