2

I posted a version of this issue earlier, but after digging more, I realized that the template parts were a bit different than I had originally thought. I've also read through similar questions on here, but the structure of the code I'm working with does not quite align with other examples.

The theme I'm working with is custom-built, not a revision of a pre-existing theme. It was built by an agency and I have ended up adopting it when the contract ended.

ISSUE

Our site's News section generates a page with post excerpts and thumbnails. At the bottom of the page is the pagination, linking to the extra pages of posts. The problem is, when the link to the second page of posts is clicked, the URL changes to /page/2 or /page/3 but the content displayed is always the same as page 1.

UPDATE

Anton was able to help get the pagination working on the index, but it has popped up again on a different template — Jobs. I've included the section of code for the job-list.php page below. I'm thinking that it will be a similar fix, but since the template lists the items differently, I do not know how to approach it.

The posts page within the template is index.php and I've included the code below. Any assistance or guidance to resources is much appreciated!

job-list.php

  <?php
  global $wp_query;
  $temp_query_holder = $wp_query;
  $job_args = array('post_type' => 'site_job');
  $wp_query = new WP_Query($job_args);

  if(have_posts()){ ?>
    <div class="job-group">
      <?php

      while(have_posts()){
        the_post();

        set_query_var('included_post', $post);
        set_query_var('last_post', ($wp_query->current_post == $wp_query->post_count - 1));
        get_template_part('template-parts/short', get_post_type());
      } ?>

    </div>

    <?php the_pagination();
  }
  else {
    get_template_part('template-parts/content', 'none');
  } ?>

index.php

<?php

get_header();
$page_id = get_option('page_for_posts');
?>

<div class="header-image-wrapper">
  <?php echo SITEPages::header_image_for_page($page_id); ?>
</div>

<section class="primary news-list list-page">
  <?php get_template_part('template-parts/breadcrumbs'); ?>
  <h1 class="page-title"><?php echo get_the_title($page_id); ?></h1>

  <?php $featured_post = get_field('featured_news_post', $page_id);

  if(!empty($featured_post)){
    set_query_var('included_post', $featured_post);
    get_template_part('template-parts/short-post-featured');
  } ?>

  <div class="post-list">
    <?php
    $news_query = new WP_Query(array(
      'post__not_in' => array($featured_post->ID)
    ));

    if($news_query->have_posts()){
      foreach($news_query->posts as $i => $news_post){
        set_query_var('included_post', $news_post);
        set_query_var('last_post', ($news_query->current_post == $news_query->post_count - 1));
        get_template_part('template-parts/short-post');
      }

      the_pagination();
    }

    else {
      get_template_part('template-parts/content', 'none');
    } ?>
  </div>
</section>

<?php
get_sidebar();
get_footer();

pagination.php

<?php
$pagination = get_the_posts_pagination(array(
  'prev_next' => false,
  'end_size' => 1,
  'before_page_number' => '<span class="page-descriptor">Page </span>'
));

if(!empty($pagination)){ ?>
  <div class="pagination-container">
    <span class="prev">
      <?php previous_posts_link('<span class="icon-chevron-left" data-grunticon-embed></span>'); ?>
    </span>

    <?php
      echo $pagination;
    ?>
    <span class="next">
      <?php next_posts_link('<span class="icon-chevron-right" data-grunticon-embed></span>'); ?>
    </span>
  </div>
<?php } ?>

1 Answer 1

2

Try to update index.php template with new WP_Query arg paged.

The code should be:

<?php
    $paged = isset(get_query_var('paged')) ? get_query_var('paged') : 1;

    $news_query = new WP_Query(array(
      'post__not_in' => array($featured_post->ID),
      'paged' => $paged
    ));

More info in codex

2
  • Thank you, Anton! That worked. I've found that the same issue is mirrored on another part of the site that uses a different template from index.php. I tried reusing this same paged arg, but I do not know where it might fit (or if it needs to be formatted differently since it is a different template). I'll make an edit to the above post with the code from the other page.
    – Phil
    Aug 24, 2017 at 18:12
  • 1
    Never mind — I figured it out! Again, I appreciate your help and answer, Anton!
    – Phil
    Aug 24, 2017 at 18:19

Your Answer

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

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