8

I'm trying to create a single page where I display a few posts on one page. So far so good. Works all fine. Now I display posts in a foreach loop where I check if they are connnected to the page.

What I need ist something like wp_get_post_terms($post->ID); but that doens't work. There are custom registered_taxonomy's.

So how can I get all taxonomy names by $post->ID?

1
  • So, you don't need to get a list of terms, but a list of the taxonomies these terms belongs to that are attached to the post? Sep 22, 2014 at 7:03

4 Answers 4

10

Ooh, thank's for the advices and help (Robert + Peter). The Term confused myself :) This worked for me:

$taxonomies=get_taxonomies('','names');
wp_get_post_terms($post->ID, $taxonomies,  array("fields" => "names"));
2
  • 1
    You should check out my answer here about taxonomies. This should help you understand which is what. Remember to accept your own answer Sep 22, 2014 at 7:11
  • 1
    @PieterGoosen thanks for your comment and link to that answer!
    – caramba
    Sep 22, 2014 at 7:14
8

For any custom post type you can easily get the current post's taxonomies list. Copy and paste the following code:

<?php if(have_posts()) : the_post();  
    $post_type = get_post_type(get_the_ID());   
    $taxonomies = get_object_taxonomies($post_type);   
    $taxonomy_names = wp_get_object_terms(get_the_ID(), $taxonomies,  array("fields" => "names")); 
    if(!empty($taxonomy_names)) :
       foreach($taxonomy_names as $tax_name) : ?>              
          <p><?php echo $tax_name; ?> </p>
       <?php endforeach;
    endif;
endif;  ?>   
4

Try get_the_term_list

<?php echo get_the_term_list( $post->ID, 'taxonomy', '', ', ' ); ?>

You can use this in loop. Also, change taxonomy in above code with yours.

4
  • gives an error: object(WP_Error)#2386 (2) { ["errors":"WP_Error":private]=> array(1) { ["invalid_taxonomy"]=>...
    – caramba
    Sep 22, 2014 at 6:35
  • You will need to change Name of the taxonomy in above code.
    – Robert hue
    Sep 22, 2014 at 6:36
  • There are a few taxonomy names, and that's what I try to find out what their names are..
    – caramba
    Sep 22, 2014 at 6:38
  • Great solution! Simple and clean!
    – Himechi90
    Sep 4, 2020 at 3:52
1
$term_list = get_the_terms($post->ID, 'your_taxonomy');
$types ='';
foreach($term_list as $term_single) {
     $types .= ucfirst($term_single->slug).', ';
}
$typesz = rtrim($types, ', ');
echo $typesz;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.