Roots Discourse

Return array map with category name and link from CPT?

Hi, maybe somebody can help me there because I can’t find solution…

I have my custom post type “websites” with registered taxonomy category “portfolio_categories”.
But I have no idea how to add properly “category name” and “permalink to cat” in this array when I loop get_posts() function.

public function portfolioLoop(){
$portfolio_items = get_posts([
‘post_type’ => ‘websites’,
‘posts_per_page’=> ‘10’,
]);

    return array_map(function ($post) {
        return [
            'content'   => apply_filters('the_content', $post->post_content),
            'thumbnail' => get_the_post_thumbnail($post->ID, 'large'),
            'title'     => apply_filters('the_title', $post->post_title),
            'permalink' => apply_filters('permalink', get_permalink($post)),
            'cat'       => get_terms('portfolio_categories', $post),
        ];
    }, $portfolio_items);

}

and my foreach blade

@foreach($portfolio_loop as $portfolio_item)
 {!! $portfolio_item['content'] !!}
 {!! $portfolio_item['cat'] !!} 
 <a href="{!! $portfolio_item['permalink'] !!}">{!! $portfolio_item['title'] !!}</a>	 
@endforeach

I try some hooks but it return empty array…

1 Like

Ok I got it,

            'categories' => get_the_term_list($post->ID, 'portfolio_categories'),

Maybe that will be useful for somebody in the future.