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.
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.
Where would this be needed, a single post page or on a category query?
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?
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." ;)
Sorry about that, I just experimented with a few different things and found something that worked. Thanks for your time!
This topic has been closed to new replies.