WordPress array output

I have the following line of code: $terms = get_the_terms( $the_post->ID, 'posts_tags' ); echo $terms; $terms = get_the_terms( $the_post->ID, 'posts_tags' ); echo $terms; The idea is to display the array in tag format, but instead, it just returns the word Array?

How can I do it? Tags should be represented as follows: 'tag1, tag2, tag3'

+2
php wordpress
source share
3 answers

to try

 foreach($terms as $term){ echo $term; } 

you can add something to separate them, for example, $echo "," or something else, but you get the idea
You can also use this

 $termsString = implode (',' , $terms); echo $termsString; 

According to the request:

Terms are really an array, see for example @ProdigySim's answer on how it looks. You can actually print them for debugging purposes with var_dump or print_r , but that will not help you in a production environment.

Assuming this is not an associative array, you can find the first tag as follows:

 echo $terms[0] 

and the rest -

 echo $terms[1] 

Up to the last ( count($terms)-1 ).
The foreach function prints each one in order. The second, as you can find in the manual , simply makes one line of them.

In the end, as you requested in the comments: Just paste this.

 $terms = get_the_terms( $the_post->ID, 'posts_tags' ); $termsString = implode (',' , $terms); echo $termsString; 
+6
source share

You are looking for print_r ()

 $a = array ('a' => 'apple', 'b' => 'banana'); print_r ($a); 

exits

 Array ( [a] => apple [b] => banana ) 
+2
source share

Arrays are an amazing feature in every programming language I have ever used. I highly recommend spending some time reading, especially if you are setting up Wordpress.

The reason this is only an echos array is because the echo can only return rows. Try print_r ($ terms) or var_dump ($ terms) for more information.

http://php.net/manual/en/language.types.array.php

+1
source share

All Articles