I have created a template to act as a default archive.php file if this was vanilla WordPress. I cannot get pagination working. I also noticed that the “read” link is linking to the current page, not the permalink of the post it’s in. I have a feeling these things are both related, and my guess is that these functions aren’t actually within the loop that is based on the custom query that I’m working with, and I just can’t figure out how to do it. Also, I have a “featured” post at the top of the page, which is the result of a separate query. Here’s my code, please help!
Controller:
namespace App\Controllers;
use Sober\Controller\Controller;
class Archive extends Controller
{
protected $acf = true;
public function archiveFeaturedPost()
{
$featured_item = get_posts([
'posts_per_page'=>'1',
'meta_query' => array([
'key' => 'featured',
'value' => true,
]),
]);
return array_map(function ($featured_item)
{
return [
'feat-content' => apply_filters('the_content', $featured_item->post_content),
'feat-permalink' => get_post_permalink($featured_item->ID),
'feat-feat-img-url' => get_the_post_thumbnail_url($featured_item->ID),
'feat-feat-img-alt' => get_the_post_thumbnail_caption($featured_item->ID),
'feat-feat-img' => the_post_thumbnail($featured_item->ID),
'feat-title' => get_the_title($featured_item->ID),
'feat-excerpt' => get_the_excerpt($featured_item->ID),
'feat-author' => get_the_author_link($featured_item->ID),
'feat-date' => get_the_date($featured_item->ID),
];
}, $featured_item);
}
public function archiveLoop()
{
$archive_items = get_posts([
'posts_per_page' => '5',
'paged' => $paged,
'meta_query' => array([
'key' => 'featured',
'value' => '1',
'compare' => '!=',
]),
]);
return array_map(function ($archive_items)
{
return [
'archive-content' => apply_filters('the_content', $archive_items->post_content),
'permalink' => get_post_permalink($archive_items->ID),
'archive-feat-img-url' => get_the_post_thumbnail_url($archive_items->ID),
'archive-feat-img-alt' => get_the_post_thumbnail_caption($archive_items->ID),
'archive-title' => get_the_title($archive_items->ID),
'archive-excerpt' => get_the_excerpt($archive_items->ID),
'archive-old-posts' => get_previous_posts_link($archive_items),
'archive-new-posts' => get_next_posts_link($archive_items),
];
}, $archive_items);
}
}
archive.blade.php:
@extends('layouts.app')
@section('content')
<!-- @ resources/views/archive.blade.php -->
{{--
Template Name: Blog
--}}
@if (have_posts())
@while (have_posts()) @php the_post() @endphp
@include('partials.hero-archive')
@endwhile
@php wp_reset_postdata() @endphp
@endif
@if (have_posts())
<div class="container700">
@while (have_posts()) @php the_post() @endphp
@include('partials.content-archive')
@endwhile
</div>
<div class="post-nav">
<div class="post-prev">{{ $archive_items['archive-old-posts'] }}</div>
<div class="post-next">{{ $archive_items['archive-new-posts'] }}</div>
{!! get_previous_posts_link() !!}
{!! get_next_posts_link() !!}
{!! get_posts_nav_link() !!}
</div>
@endif
@if (!have_posts())
<div class="container700">
<div class="alert alert-warning">
{{ __('Sorry, no results were found.', 'sage') }}
</div>
{!! get_search_form(false) !!}
</div>
@endif
@endsection
content-archive.blade.php:
<!-- @ resources/views/partials/content-archive.blade.php -->
@foreach ($archive_loop as $archive_items)
<div class="post-excerpt wow fadeInUp">
<article @php post_class() @endphp>
<header>
<h2 class="entry-title"><a href="{{ $archive_items['permalink'] }}">{{ $archive_items['archive-title'] }}</a></h2>
<div class="feat-img-container">
<img class="feat-img" src="{{ $archive_items['archive-feat-img-url'] }}" alt="{{ $archive_items['archive-feat-img-alt'] }}" />
</div>
</header>
<div class="entry-summary">
<p>{!! $archive_items['archive-excerpt'] !!}</p>
</div>
</article>
</div>
@endforeach