Get categories in WP based on post ID


<?php
 $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[] = “<b>” . $taxonomy->label . “</b>: “;
      foreach ( $terms as $term ) {
        $out[] = $term->name.”, “;
      }
 $out[] = “<br>”;
    }
$out[] = “\n”;
  }
  echo implode(”, $out );
?>
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s