Refactor blade partial

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>
Output screenshot

full source:

Hi,

I am new and now I learn sage, but maybe that will be helpfully for you.
I think you should to create a controller.

ExampleArchive.php controller

namespace App\Controllers;

use Sober\Controller\Controller;

class ExampleArchive extends Controller
{
public function portfolioLoop(){
$portfolio_items = get_posts([
‘post_type’ => ‘NAME OF YOUR POST TYPE’,
‘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)),
        ];
    }, $portfolio_items);

}

}

Next in file where you try to output your data you have to create @foreach with function name from your controller like below

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

thanks @Jacek , I refactored my blade template and controller, they look much cleaner now!

2 Likes

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