1

I have written my custom WP_Query and using loop to display post content. I use get_the_category() to display categories of current post and it works fine. Now for some post types there are custom taxonomies instead of categories.

Code to get categories:

$categories = get_the_category();
   if(!empty($categories)){
      foreach($categories as $index => $cat){
         echo $cat->name;
      }
   }

Now I need to pull all taxonomies and print them in comma separated format.

I tried this:

$taxonomies = get_the_taxonomies();
if(!empty($taxonomies)){
   foreach($taxonomies as $taxonomy){
         echo $taxonomy;
   }
}

It works and shows in this format "Taxonomy Label: Term (hyperlinked)". If the terms are more than one than it adds "and" between terms. I need only terms and if they are multiple then they should be separated by comma.

I want to know:

  1. The best approach to achieve those results
  2. Is it recommended to to use above method?
  3. Can I use regex to extract value?
  4. How can I get rid off hyperlink?

Thanks

  • Do you want to print taxonomies or terms? get_the_taxonomies will get you taxonomies (like: category, post_tag, post_format) and not terms from that taxonomies... – Krzysiek Dróżdż Nov 18 '18 at 11:31
1

The first problem with your code, I guess, is that you use get_the_taxonomies function, which will:

Retrieve all taxonomies of a post with just the names.

So its result will be like this:

Array
(
    [0] => category
    [1] => post_tag
    [2] => post_format
)

And I'm pretty sure that you want to get terms assigned to given post from all taxonomies, and not the taxonomies names...

So most probably you want to do something like this:

$terms = wp_get_object_terms( get_the_ID(), array_keys( get_the_taxonomies() ) );
foreach ( $terms as $i => $term ) {
    echo ($i ? ', ' : '') . $term->name;
}

And quick answers to your questions:

  1. One of possible solutions above - it's hard to say if it's the best one.
  2. No, your method is not a solution, I guess.
  3. There is no need to use regex. You should avoid using regex when there is no need for that.
  4. You can get rid of hyperlinks by getting term objects and printing them by yourself (as shown above).
  • thank you so much for your answer, the code does not work. It throws an error "Trying to get property of non-object" for echo line. And when I try to print $terms object, it returns error "invalid taxonomy". What's could be wrong? – Alena Nov 18 '18 at 11:55
  • @Alena updated my answer, one function call was missing... – Krzysiek Dróżdż Nov 18 '18 at 12:11
  • Absolutely awesome, it worked now. Can you explain a bit about ternary statement, which is being used in echo statement? – Alena Nov 18 '18 at 12:17
  • @Alena yes. This statement checks if $i is greater than 0 and returns “, “ in such case or empty string otherwise. So it will add comma before every item except the first one. – Krzysiek Dróżdż Nov 18 '18 at 12:19
  • @Alena no problem :) – Krzysiek Dróżdż Nov 18 '18 at 12:56
0

Take a look at these:

$taxonomies = get_post_taxonomies( );
print_r( $taxonomies );
echo implode( $taxonomies, ', ' );
  • 1
    Thanks for your answer, but it doesn't work. How is it suppose to remove hyperlink? – Alena Nov 18 '18 at 11:26
  • It doesn't need to... Instead of using get_the_taxonomies, I used get_post_taxonomies. – De Coder Nov 18 '18 at 12:26

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.