0

I need to create an Array that contains all of the children slugs of a specific parent category ID.

Let's say I have the following parent category and its children categories:

  1. Food
    • pizza
    • bread
    • banana
    • ice cream

I need a code/function to use inside functions.php that returns the slugs of all of the children's slug of a specific parent category like this:

function-get-child-slug( $parentID='1');

It returns this:

array('pizza','bread','banana','ice-cream')

I've tried some variants of this but it didn't work:

$var = wp_list_categories( array( 
        'child_of'           => 1,
         'echo'              =>false
    ) ); 
$array = array(strip_tags($var));

How can I achieve this?

0

You can use get_terms() to get an array of child slugs:

/**
 * Get child term slugs from given term parent
 *
 * @param Integer $parent_id ( Parent Term ID )
 * @param String $taxonomy ( Taxonomy Slug )
 *
 * @return Array( $term_id => $child_slug )
 */
function get_child_slugs( $parent_id = 0, $taxonomy = 'category' ) {
    return get_terms( array(
        'taxonomy'  => $taxonomy,
        'parent'    => $parent_id,
        'fields'    => 'id=>slug',
    ) );
}

This should return a simple array of array( $term_id => $term_slug ) assuming a valid parent term ID and valid taxonomy is supplied.

0

There's this get_term_children function you can use to list the child categories of a given category, it returns the IDs of child cats if found, and get_term can be used to get category information rather than just an ID.

function get_child_slug($parent=1, $tax='category') {
    if ( !is_numeric($parent) ) {
        $parent = get_term_by('name', $parent, $tax);
    } else {
        $parent = get_term( $parent );
    }

    if ( empty( $parent->term_id ) )
        return array();

    $children = get_term_children( $parent->term_id, $tax );

    if ( $children ) {
        $children = array_map('get_term', $children);
    }

    return $children;
}

Here's a dump test:

samuel@Samuel-PC:~/htdocs/multisite.dev/www$ wp eval 'print_r(get_child_slug("Food"));'
Array
(
    [0] => WP_Term Object
        (
            [term_id] => 3
            [name] => pizza
            [slug] => pizza
            [term_group] => 0
            [term_taxonomy_id] => 3
            [taxonomy] => category
            [description] => 
            [parent] => 2
            [count] => 0
            [filter] => raw
        )

    [1] => WP_Term Object
        (
            [term_id] => 4
            [name] => bread
            [slug] => bread
            [term_group] => 0
            [term_taxonomy_id] => 4
            [taxonomy] => category
            [description] => 
            [parent] => 2
            [count] => 0
            [filter] => raw
        )

    [2] => WP_Term Object
        (
            [term_id] => 5
            [name] => banana
            [slug] => banana
            [term_group] => 0
            [term_taxonomy_id] => 5
            [taxonomy] => category
            [description] => 
            [parent] => 2
            [count] => 0
            [filter] => raw
        )

    [3] => WP_Term Object
        (
            [term_id] => 6
            [name] => ice cream
            [slug] => ice-cream
            [term_group] => 0
            [term_taxonomy_id] => 6
            [taxonomy] => category
            [description] => 
            [parent] => 2
            [count] => 0
            [filter] => raw
        )

)

Hope that helps.

  • Thank you Samuel Elh! I ended up using Howdy's solution, your array has more information than what I need. But I'm saving your code for future references. Thank you! – A.T Jan 26 '17 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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