Get the Tags

Hey,

i am new with sage. I want to get the tags from the post.

for CPT I use a function:

public function ReviewItems() {
$review_items = get_posts([
‘post_type’ => ‘reviews’,
‘posts_per_page’=>‘100’,
‘orderby’=>‘date’,
‘order’ => ‘ASC’,
]);

            return array_map(function ($post) {
            return [
            'title'     =>  apply_filters('the_title', $post->post_title),
            'date'      =>  get_the_date(('d / F'), $post->ID),
            'excerpt'   =>  apply_filters('the_excerpt', $post->post_excerpt),
            'thumbnail' =>  get_the_post_thumbnail($post->ID, 'medium'),
            'link'      =>  get_the_permalink($post->ID),
            'aria'      =>  apply_filters('the_title','aria-label='.$post->post_title),
            'name'      =>  apply_filters('the_title','title='.$post->post_title),
            'from'      =>  get_field( 'review_author', $post->ID ),
            'terms' => get_the_tags( $post->ID),

            ];
            }, $review_items);
        }

FRONTEND:

{!! $review_item[‘terms’] !!}

returns only “ARRAY”

what is the correct way to get the array?

get_the_tags() returns an array of term objects. You can’t echo that array directly (what would it print?) so you need to parse it into whatever form you want first. The docs pages for that function provides an example:

<?php
$posttags = get_the_tags();
if ($posttags) {
  foreach($posttags as $tag) {
    echo $tag->name . ' '; 
  }
}
?>

There is also a more general function that prints the post tags using the default WordPress tags function/template: https://codex.wordpress.org/Function_Reference/the_tags

Thanks , Bit Not Clear …

$review_item[‘terms’] is an array which is what is outputting when you are echoing the variable in your template. As @alwaysblank mentioned, you need to loop through the array and get the string values of the term names which are stored as properties of each term object (the array is an array of term objects.

This topic was automatically closed after 42 days. New replies are no longer allowed.