skip to Main Content

I am trying to get the parent category and child category from the current post.
The custom post type is called assortiment and the custom taxonomy is called assortiment-categorie.

We have a product called SL524CB – 500kg with parent category Test and subcategory (of Test) test b.

Now I did make a loop which outputs the sub category name (test b) but we also want the parent category name (Test).

To output them we want 2 variables like $parent_category and $subcategory so we can output them in our template.

This is the loop we are using right now:

<?php 
    global $post;
    $terms = wp_get_object_terms( $post->ID, 'assortiment-categorie', array('fields'=>'names'));
    $term_id = array_pop( $terms ); //gets the last ID in the array
    echo $term_id;
?>

If someone could help me that would be great, thanks for your time already!

2

Answers


  1. There is a multitude of ways we can achieve that.

    @Chris Haas comment is one way to do it. Me, I prefer using an alternative way.

    Regarding parents, we can use get_term_parents_list() which will return a specific term’s parents.

    Retrieves term parents with separator.

    <?php
    
    $args = array(
        'format' => 'slug',
        'link' => false,
        'inclusive' => false,
    );
      
    $parents = explode( '/', get_term_parents_list( $term_id, $taxonomy, $args ) );
    
    $î = 0;
    foreach( $parents as $parent ) {
        $i++;
    
        echo $parent;
    
        if ( $i !== sizeof( $parents ) )
            echo ', ';
    
    };
    

    Regarding children, we can use get_term_children() which will return a specific term’s children.

    Merge all term children into a single array of their IDs.

    <?php
    
    $children = get_term_children( $term_id, $taxonomy );
    
    $î = 0;
    foreach( $children as $child ) {
        $i++;
    
        $term = get_term_by( 'id', $child, $taxonomy );
    
        echo $term->name;
    
        if ( $i !== sizeof( $children ) )
            echo ', ';
    
    };
    
    Login or Signup to reply.
  2. Use get_the_terms().

    $categories = get_the_terms( get_the_ID(), 'category' );
    echo "<pre>"; print_r($categories); echo "</pre>"; 
    

    If you print $categories then you will have an output like below. where you can determine that $term is parent or child.

    Array
    (
        [0] => WP_Term Object
            (
                [term_id] => 18
                [name] => Test 2
                [slug] => test-2
                [term_group] => 0
                [term_taxonomy_id] => 18
                [taxonomy] => category
                [description] => 
                [parent] => 17
                [count] => 1
                [filter] => raw
            )
    
        [1] => WP_Term Object
            (
                [term_id] => 17
                [name] => Test
                [slug] => test
                [term_group] => 0
                [term_taxonomy_id] => 17
                [taxonomy] => category
                [description] => 
                [parent] => 0
                [count] => 1
                [filter] => raw
            )
    
    )
    

    Try this below code.

    foreach ( $categories as $key => $category ) {
        if( $category->parent == 0 ){
            echo "Parent => ".$category->name;
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search