Roots Discourse

Get_posts_nav_link() and other pagination methods, get_the_excerpt() "read more" link trouble

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

The way you’ve set this up is kind of strange. I’d recommend carefully reading through the documentation for Controller, as well as the WordPress documentation on The Loop. You set up several variables in your controller, but never use them. When you call the content-archive.blade.php partial inside of the archive post loop, you then use it to run through an array instead of display post information. It’s unclear whether you intended for this template to be used as the archive template (which WordPress will try to do because you’ve named the file archive) or whether you want to use it as a selectable page template (as your use of the Template Name: comment suggests). Any of these things could be contributing to your problems.

Your issues with pagination are likely coming from the fact that your list of posts is coming from an array returned by get_posts, not the main archive query. Pagination will only work the way you’re expecting it to if you’re using it on a true archive endpoint and using the main query to display your posts.

Thank you. Yes, there are variables in there that I’m not currently using. I had set them up thinking I might need them but ended up not, should have removed them before posting. My apologies.

The reason I set this up as a selectable template was because I couldn’t get WordPress to recognize it as the default archive template I’ve tried titling the file archive.blade.php and archive.php (the latter is how it is titled in the Controller documentation, which I’ve reviewed at your suggestion and am using to rework my code) in the resources/views directory, but it keeps using index.blade.php. As a selectable template, I was able to force it to use this template. I’ll keep working on it, but any pointers on working with archive.php or its equivalent using Sage would be much appreciated.

A file named archive.blade.php in [theme]/resources/views will be picked up by WordPress as an archive template. I’ve done this many times. If it isn’t being picked up, something else is interfering with it.

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