I am trying to refactor two foreach
loops at recently-added.blade.php
template:
@foreach (wp_get_post_terms(get_the_ID(), ['petplace-type']) as $category)
<a class="" href="#">{!! $category->name !!}</a>
@endforeach
@foreach (wp_get_post_terms(get_the_ID(), ['tag']) as $tag)
<a class="..." href="#">#{!! $tag->name !!}</a>
@endforeach
I want them to look like this:
@foreach ($categories as $category)
<a class="" href="#">{!! $category !!}</a>
@endforeach
@foreach ($tags as $tag)
<a class="" href="#">#{!! $tag !!}</a>
@endforeach
How do I get the custom post term names into $tags
and $categories
array and pass these arrays to my template?
below is my current code and output screenshot
FrontPage.php
<?php
namespace App\Controllers;
use Sober\Controller\Controller;
class FrontPage extends Controller {
public function latest_petplaces() {
$args = array(
'post_type' => 'petplace',
'orderby' => 'date',
'order' => 'desc',
'posts_per_page' => 3,
);
$the_query = new \WP_Query($args);
return $the_query;
}
}
recently-added.blade.php
<div class="text-center">
<h2>Recently Added</h2>
</div>
<div class="block md:flex justify-between md:-mx-2">
@while($latest_petplaces->have_posts()) @php($latest_petplaces->the_post())
<div class="w-full lg:w-1/3 md:mx-2 mb-4 md:mb-0">
<div class="bg-white rounded-lg overflow-hidden shadow relative">
<a href="{{ get_permalink() }}">{!! the_post_thumbnail(null, ['class' => 'h-56 w-full object-cover object-center']) !!}</a>
<div class="p-4 h-auto md:h-40 lg:h-48">
<a href="{{ get_permalink() }}" class="block text-blue-500 hover:text-blue-600 font-semibold mb-2 text-lg md:text-base lg:text-lg">{!! get_the_title() !!}</a>
<div class="text-gray-600 text-sm leading-relaxed block md:text-xs lg:text-sm">
@foreach (wp_get_post_terms(get_the_ID(), ['petplace-type']) as $category)
<a class="" href="#">{!! $category->name !!}</a>
@endforeach
{!! the_excerpt() !!}
</div>
<div class="relative mt-2 lg:absolute bottom-0 mb-4 md:hidden lg:block">
@foreach (wp_get_post_terms(get_the_ID(), ['tag']) as $tag)
<a class="inline bg-gray-300 py-1 px-2 rounded-full text-xs lowercase text-gray-700" href="#">#{!! $tag->name !!}</a>
@endforeach
</div>
</div>
</div>
</div>
@endwhile
@php(wp_reset_postdata())
</div>
full source: