0

The code i'm using for displaying custom taxonomies is as below:

function custom_taxonomies_terms_links(){
  // get post by post id
  $post = get_post( $post->ID );

  // get post type by post
  $post_type = $post->post_type;

  // get post type taxonomies
  $taxonomies = get_object_taxonomies( $post_type, 'objects' );

  $out = array();
  foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){

    // get the terms related to post
    $terms = get_the_terms( $post->ID, $taxonomy_slug );

    if ( !empty( $terms ) ) {
      $out[] = "<br/>".$taxonomy->label . " : ";
      foreach ( $terms as $term ) {
        $out[] =
          '  <a href="'
        .    get_term_link( $term->slug, $taxonomy_slug ) .'">'
        .    $term->name
        . "</a>";
      }
      $out[] = " ";
    }
  }

  return implode('', $out );
}

However it is also showing the category taxonomies and tag taxonomies, which are default taxonomies and i only want to display the custom ones. Which modification of the code should i make? I tried to create an arraty using category and post_tag slugs and used array_diff, but it didn't work cause there is some array vs object conflict happening.

0

Each taxonomy object will have _builtin set to 1 for native taxonomies, so check $taxonomy->_builtin in your foreach loop first before fetching the terms.

0

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.