I have 2 levels of categories ( one sub-category only. No sub-sub
category ). I need to get all sub categories without parent category. (I don’t want to specify any parent category.)
Example:
p_cat1
s_cat1
s_cat2
p_cat4
p_cat3
s_cat4
s_cat5
s_cat7
from here, I need
s_cat1
s_cat2
s_cat4
s_cat5
s_cat7
And, I need there to be a URL for each of those sub-categories. I want to give <a>
for each subcategory.
How do I do that?
You could use
get_categories()
.This is a very simple example of the code without extra parameters like
hide_empty
,type
etc.The get_categories function of WordPress returns all the categories, you can loop over the list of categories returned by get_categories and check for the
parent
property, if parent is ‘0’ then its the parent category other wise it is the sub category.I don’t know how you want with URL but you can try the following function which will only return Sub categories name hyper linked to their category URLs