0

Hello,

I have a 'Custom Post Type' with 8 taxonomies and I'm pretending display a list with only 6 taxonomies and the no empties terms of each one and hide 2 taxonomies.

My current code displays all taxonomies with all terms hiding the empties one but I can not get how to hide the two taxonomies I want.

This is my code:

 <? php
  $args = array(
    'public'   => true,
    '_builtin' => false
    );
  // $output = 'names'; // or objects
  // $operator = 'and'; // 'and' or 'or'
  // $taxonomies = get_taxonomies( $args, $output, $operator );
  $object = 'my-cpt-name';
  $output = 'names';
  $taxonomies = get_object_taxonomies( $object, $output );
  if ( $taxonomies ) {
    foreach ( $taxonomies  as $taxonomy ) {
      echo '<h3>' . $taxonomy . '</h3>';

      $args = array( 'hide_empty=0' );

      $terms = get_terms( $taxonomy, $args );
      if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
         $count = count( $terms );
         $i = 0;
         $term_list = '<ul class="term-list">';
         foreach ( $terms as $term ) {
             $i++;
             $term_list .= '<li><a href="' . esc_url( get_term_link( $term ) ) . '" >' . $term->name . '</a></li>';
             if ( $count != $i ) {
             }
             else {
                 $term_list .= '</ul>';
             }
         }
         echo $term_list;
      }
    }
  }
?>

I have set up the 'public' taxonomy $arg as 'false' but doesn't work.

$args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => false,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
);

And also, how could I display the taxonomy name instead of the taxonomy slug on echo '<h3>' . $taxonomy . '</h3>';

  • To clarify, you want to list all taxonomies (and their terms) associated with the your custom post type, but hide two of them and any terms that have 0 counts? – darrinb Apr 20 '16 at 19:40
2

Try this:

<?php
$object = 'post';
$output = 'objects';
$taxonomies = get_object_taxonomies( $object, $output );
$exclude = array( 'post_tag', 'post_format' );

if ( $taxonomies ) {

    foreach ( $taxonomies  as $taxonomy ) {

        if( in_array( $taxonomy->name, $exclude ) ) {
            continue;
        }

        $terms = get_terms( array(
            'taxonomy' => $taxonomy->name,
            'hide_empty' => true,
        ) );

        if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {

            echo '<h3>' . $taxonomy->label . '</h3>';   

            $term_list = '<ul class="term-list">';

            foreach ( $terms as $term ) {
                $term_list .= '<li><a href="' . esc_url( get_term_link( $term ) ) . '" >' . $term->name . '</a></li>';          
            }

            $term_list .= '</ul>';

            echo $term_list;
        }

    }

}
?>

For this example I set up some parameters:

  • Post type we're searching on: $object = 'post';
  • Taxonomies we want to exclude: $exclude = array( 'post_tag', 'post_format' );

Then we get all associated taxonomies and loop through them. While we loop through them, we check each taxonomy against our exclude array and skip it if applicable.

Then we grab all the terms from each taxonomy and build list of links.

  • Thanks for your help @darrinb I just try it but shows a blank page. – andresgl Apr 20 '16 at 20:10
  • Did you change the $object to your post type? – darrinb Apr 20 '16 at 21:38
  • Hmm. I tested on my site exactly what I posted and it worked as expected. Are you getting any error messages? – darrinb Apr 20 '16 at 23:14
  • Hello darrinb, No, I'm not getting any error. I have MAMP and WP on mode debig and doesn't return any error. The debug mode is working because if I cause a error the error is showed. Thanks! – andresgl Apr 21 '16 at 12:43
0

@darrinb

I have solved the problem. Your code helped me a lot although had a error. :)

The error was getting the 'terms':

$terms = get_terms( array(
            'taxonomy' => $taxonomy->name,
            'hide_empty' => true,
        ) );

Following the codex:

The correct way is the following:

$arg = array(
        'hide_empty' => true,
        );
$terms = get_terms($taxonomy->name, $arg );

Thank you!

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.