skip to Main Content

Is it possible to hide taxonomies without categories. I’m using get_object_taxonomies to show all my taxonomies inside my custom post type.

<?php
            
        $blog_taxonomies = get_object_taxonomies( 'blog', 'textdomain',
        (array(
            'hide_empty' => true,
        ))
    );
        
        foreach ($blog_taxonomies as $blog_taxonomy) : 
        
        ?>

            <ul class="blog__categories-list">

                <li class="blog__categories-title"> <?= $blog_taxonomy->labels->name; ?> </li>

                <?php endforeach; ?>

2

Answers


  1. You can use the code below. The parameters you submitted to get_object_taxonomies are incorrect.

    $taxonomy_objects = get_object_taxonomies('blog', 'objects');
            foreach ($taxonomy_objects as $taxonomy) {
                $taxonomy_terms = get_terms(['taxonomy' => $taxonomy->name, 'hide_empty' => true]);
                if (!empty($taxonomy_terms)) {
                    return;
                }
                // Your operation
            }
    

    See get_object_taxonomies documentation.

    Login or Signup to reply.
  2. use get_object_taxonomies function like this

    $taxonomy_objects = get_object_taxonomies( array( 'post', 'product' ), 'objects' );
    foreach ( $taxonomy_objects as $taxonomy ) {
        $taxonomy_terms = get_terms( array( 'taxonomy' => $taxonomy->name, 'hide_empty' => true ) );
        //var_dump($taxonomy_terms);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search