0

i have a problem understanding the get_terms function

All i want is getting all the children of specific category so that it can be listed. But i don't really know how the $args works.

So with this get_terms(category, $args);

I tried to use the child_of

$args = array(
        'child_of'          => 0,
        'childless'         => false,
);

But it gives all the category while i only need the child of a category. Changing the value to "1" gives empty result.

Then i tried the 'slug'

$args = array(
        'slug'              => 'on-going',
        'child_of'          => 0,
        'childless'         => false,
); `

This gives nothing but "On-going" only.

This is the category tree in my wordpress

category

The only thing i want is listing all child category of "On-going", that's it....

Thanks

1 Answer 1

1

The child_of parameter excepts an integer value of the specific term you need to get descendants from. This means that you need to get the ID from the On-going category term and then passing that ID to the child_of parameter.

To note, the child_of parameter returns all descendants of the term passed, this includes direct children and all children of those children etc. If you only need first level children of a specific term, then you need to pass the term id to the parameter parent.

If you do not have the exact id, you can use get_term_by to get the id and then pass that to child_of in get_terms

EXAMPLE

This example shows how to get the term ID if you only have the term name, like in your case On-going

$get_term_id = get_term_by( 'name', 'On-going', 'category' );
$terms = get_terms( 'category', array( 'child_of' => $get_term_id->term_id ) );
var_dump( $terms );
7
  • My apologize before, this script worked, but there's a small problem, it returns everything like description slug, count, and others.. i tried to use the "exclude" arg, but again, i don't know how it works... my webpage doesn't load when i add the exclusion.. or am i doing wrong with this? 'exclude' => description, slug
    – Yahya98
    Commented Jun 30, 2015 at 3:59
  • If you need something specific returned, look at the fields parameters. You need to understand, get_terms returns an object of terms, so you need a foreach loop to print the names of the categories Commented Jun 30, 2015 at 4:17
  • I'm very sorry of my stupidness, but i don't even understand the script.. i'm using the script you gave me in my previous question.. pastebin.com/b3zwDzFG which one should i remove to exclude the whole thing except the title?
    – Yahya98
    Commented Jun 30, 2015 at 4:25
  • Do you need to build a tree Commented Jun 30, 2015 at 4:33
  • Uhm, i wanted to create a page with categories listed in alphabetical order.. prntscr.com/7mzf71
    – Yahya98
    Commented Jun 30, 2015 at 4:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.