1

In my WooCommerce theme I've created a root category: 'Brands' then for each product I've added it's brand as a sub category to the 'Brands' parent category.

On the product page I want to display the brand of the current product.

global $post;

// Get the brands ID from slug
$brands_id = get_term_by('slug', 'brands', 'product_cat');

// Get the children of that term
$termchildren = get_term_children( $brands_id, 'product_cat' );

// Loop through the children of 'brand' term to echo the name of the brand
foreach ( $termchildren as $child ) {
    $term = get_term_by( 'id', $child, 'product_cat' );
    echo '<a href="' . get_term_link( $child, 'product_cat' ) . '">' . $term->name . '</a>';
}

This always echoes all the brands. How do I take this information and filter out the brands not associated with the current product?

  • Why are you assuming it should filter like that? I don't remember args taking term_id there. – Rarst Apr 20 '14 at 12:56
  • Yes it looks like you're right, I was getting confused with the return array – tommyf Apr 20 '14 at 13:32
  • Resolved then? :) Or please update your question. – Rarst Apr 20 '14 at 14:36
  • Question updated. – tommyf Apr 20 '14 at 19:30
3

Don't need get_term_children() at all. Just loop through get_the_terms() to get what's needed.

global $post;
$brands_id = get_term_by('slug', 'brands', 'product_cat');

$terms = get_the_terms($post->ID, 'product_cat');
foreach ($terms as $term) {
    if($term->parent === $brands_id->term_id) {
        echo $term->name;
        break;
    }
}
1
$term_id       = get_term_by( 'slug', 'brands', 'product_cat' );
$taxonomy_name = 'product_cat';
$termchildren  = get_term_children( $term_id, $taxonomy_name );

echo '<ul>';
foreach ( $termchildren as $child ) {
    $term = get_term_by( 'id', $child, $taxonomy_name );
    echo '<li><a href="' . get_term_link( $child, $taxonomy_name ) . '">' . $term->name . '</a></li>';
}
echo '</ul>';
0

First of all I have asked the same question here and I got the answer myself :)

https://wordpress.stackexchange.com/questions/188839/wordpress-tags-and-hierarchy-parent-child-relation

Here is how you should do it:

    $taxonomies = array( 
        'brands'
    );

    $args = array(
        'orderby'           => 'name', 
        'order'             => 'ASC',
        'fields'            => 'all',
        'parent'            => '(parentID)',
        'hierarchical'      => true, 
        'child_of'          => 0
    ); 

    $terms = get_terms($taxonomies, $args);
    var_dump($terms);
    foreach ($terms as $term) {
        print '<h2 class="story-heading">'.$term->name.'</h2>';
    } 

if you already know the ID for the parent category you just pass that as INT and into the foreach cycle you will get all the child terms (subcategories) of the parent. Also if you need to reuse this I mean if you want to display multiple times the same query and get different parent -> child terms I would recommend writing a function and placing it into the functions.php the function should look something like this:

function get_children_of_parent_terms($tax, $pid) {
  $taxonomies = $tax;

        $args = array(
            'orderby'           => 'name', 
            'order'             => 'ASC',
            'fields'            => 'all',
            'parent'            => $pid,
            'hierarchical'      => true, 
            'child_of'          => 0
        ); 

        $terms = get_terms($taxonomies, $args);
        var_dump($terms);
        foreach ($terms as $term) {
            print '<h2 class="story-heading">'.$term->name.'</h2>';
        }
   }

add_action('init','get_children_of_parent_terms');

And then whenever you want to query the children from the parent term(category) you just call this function with the parent category name of the taxonomy as $tax, and the parent id of as $pid for example echo get_children_of_parent_terms('brands','15'); and assuming that you have the taxonomy brands and some parent term with id 15 that will output all the child terms of the term with ID 15.

Cheers.

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.