0

While there are many posted solutions on how to insert custom components between posts, I haven't been able to find one that addresses pagination. Currently, on my Wordpress archive template, each page shows 12 posts.

While I'm able to insert a custom component after the 2nd post, given the example below, if I also want to add an additional component that is specified after the 13th post, it will never appear since WP starts the count over again. In addition, the same component is returned on every page.

I assume I need to know the total number of pages in advance so there's a number to check against?

Currently:

@extends('layouts.app')
@section('content')
@include('partials.page-header')

@php 
$global = $wp_query;
$count = 1;

// get total pages?
$total_pages = $wp_query->max_num_pages;
@endphp


@if (!have_posts())
  {{ __('Sorry, there are no posts.', 'sage') }}
  {!! get_search_form(false) !!}
@endif

@while (have_posts()) @php the_post() @endphp

  @include('partials.content-'.get_post_type())

  @if ($count == 2 )
  @include('partials.component_1')
  @endif

  @if ($count == 13 )
  @include('partials.component_2')
  @endif

  @php $count++ @endphp

@endwhile

{!! pagination_bar() !!}
@endsection
2
  • 1
    What is this @ syntax in your code?
    – Tom J Nowell
    Commented Nov 3, 2022 at 16:03
  • That's part of Blade's syntax language which is primarily used with Laravel. I'm using the Sage theme roots.io which uses Laravel components and templates
    – scottrod
    Commented Nov 5, 2022 at 17:15

1 Answer 1

0

I wouldn't do this with a "count" that is reset on the loop, but instead use some of the items in the WP_Query object, like paged value. You can use that to interpret that you are on the second or subsequent pages and then if you are x item on y page (or some other format) you should be solid.

Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.