0

I am searching for a solution to display a grid for a custom post type with a specific taxonomy that display latest 4 posts on a page (it could be a template page in my custom child theme) then on another page template the next 4 posts are shown.

I need 4 separate distinct pages to display 16 posts, each page containing 4 posts. First page = latest 4 posts, second page next 4 posts, third page next 4 posts from page 2 and the 4th page display the next 4 posts from page 3.

I did manage to display the latest 4 posts, but I'm stuck at how to display the next 4 posts on another page.

Using this code created a paged version of this listing, is there any way to duplicate the same template for page 2 and display the page directly from pagination second step ? My goal is to make 4 distinct pages. If there is another way I thank anyone who can point me in the right direction to learn how to do this.

Many thanks, don't hesitate to ask for explanations if the question is ambiguous.

    <?php
/**
 * Template Name: Custom Query page 01
 * The custom page template file
 */
?>

<?php get_header(); ?>

<h2>List the first 4 posts</h2>

<?php 

  $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

  $custom_args = array(
      'post_type' => 'listings',
      'tax_query' => array(
        array(
            'taxonomy' => 'custom_taxonomy',
            'field' => 'slug',
            'terms' => $custom_term->slug,
        ),
    ),
      'posts_per_page' => 4,
      'paged' => $paged
    );

  $custom_query = new WP_Query( $custom_args ); ?>

  <?php if ( $custom_query->have_posts() ) : ?>

    <!-- the loop -->
    <?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
      <article class="loop">
        <h3><?php the_title(); ?></h3>
        <div class="content">
          <?php the_excerpt(); ?>
        </div>
      </article>
    <?php endwhile; ?>
    <!-- end of the loop -->

    <!-- pagination here -->
    <?php
      if (function_exists(custom_pagination)) {
        custom_pagination($custom_query->max_num_pages,"",$paged);
      }
    ?>

  <?php wp_reset_postdata(); ?>

  <?php else:  ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
  <?php endif; ?>

<?php get_footer(); ?>

The function that do the pagination (that I added to functions.php):

function custom_pagination($numpages = '', $pagerange = '', $paged='') {

  if (empty($pagerange)) {
    $pagerange = 4 ;
  }

  global $paged;
  if (empty($paged)) {
    $paged = 1;
  }
  if ($numpages == '') {
    global $wp_query;
    $numpages = $wp_query->max_num_pages;
    if(!$numpages) {
        $numpages = 1;
    }
  }

  /** 
  * Construct of the pagination arguments to enter into paginate_links       */
  $pagination_args = array(
    'base'            => get_pagenum_link(1) . '%_%',
    'format'          => 'page/%#%',
    'total'           => $numpages,
    'current'         => $paged,
    'show_all'        => False,
    'end_size'        => 1,
    'mid_size'        => $pagerange,
    'prev_next'       => True,
    'prev_text'       => __('&laquo;'),
    'next_text'       => __('&raquo;'),
    'type'            => 'plain',
    'add_args'        => false,
    'add_fragment'    => ''
  );

  $paginate_links = paginate_links($pagination_args);

  if ($paginate_links) {
    echo "<nav class='custom-pagination'>";
      echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
      echo $paginate_links;
    echo "</nav>";
  }
}
7
  • Is this the only place you're listing the CPTs from this taxonomy, or is it in addition to their normal archive?
    – Chris Cox
    Commented Sep 29, 2017 at 18:13
  • Hello, this is a simple custom page template, I use it on a blank page where I use this template file as page template. Commented Sep 29, 2017 at 18:23
  • I can see that it's a custum page template, but do you also use the normal archive template for the taxonomy?
    – Chris Cox
    Commented Sep 29, 2017 at 18:25
  • No, I didn't alter the default archive.php that reside in main theme, I use a child theme. Commented Sep 29, 2017 at 18:28
  • Could you not just use the built in archive at /custom_taxonomy/ and the taxonomy-custom_taxonomy.php template? Then it'd be a standard post loop with normal pagination
    – Tom J Nowell
    Commented Sep 29, 2017 at 18:33

1 Answer 1

0

I frequently see Page templates like this when someone's trying to create a custom loop - possibly inspired by the built-in functionality of creating a Page as a stub for Posts when another Page is set as the front page for the site. That's a special case, and not what people should be doing.

Instead of using a Page template and running a second query in place of the content, what you should be doing is creating taxonomy-custom_taxonomy.php and altering the default WP_Query before it's sent to the DB by hooking pre_get_posts to get the result you want. Here's the sort of function you'd want in your child theme's functions.php:

add_action( 'pre_get_posts', 'wpse281501_taxonomy_query' );

function wpse281501_taxonomy_query($query) {
    if($query->is_main_query() && $query->is_tax( 'custom_taxonomy' ) {
        $query->set( 'posts_per_page', 4 );
    }
}

Taxonomy archives support pagination by default, whereas a Page template requires kludging support in because Pages aren't paginated.

If you're only looking to apply this layout to a specific term within the taxonomy you'd need to call your template taxonomy-custom_taxonomy-custom_term.php and pass the term as the second argument to is_tax() in your hooked function:

if($query->is_main_query() && $query->is_tax( 'custom_taxonomy', 'custom_term' )
5
  • Using the method you just write, how can I create 4 distinct pages to display the posts ? I don't only want the latest 4 posts, I want the 4 latest on first page then next 4 on second and so till page 4 (total of 16 posts). I was wondering in creating one array with all the ids of the query (custom post & taxonomy) and use one loop / page with array[0], array[1], array[2], array[3] for page 1 where array[0] is the first ID, using this valor for post ID. Many thanks for the answer I will try to make it this way. I will come back with my result, once again thank you. Commented Sep 29, 2017 at 19:05
  • As I mentioned, archives support pagination by default so appending /page/2 to the URL should take you to the next 4 posts because the posts_per_page argument is set to 4 by the hooked function.
    – Chris Cox
    Commented Sep 29, 2017 at 19:17
  • Omg ! Didn’t make the parallel with the pagination permalink! I’l give it a try and come back with the result ! Commented Sep 29, 2017 at 19:21
  • Thank you very much @Chris Cox for your time and the answer.Your solution works like a charm, I was able to dispay the posts by group of 4 in a listing style, now I have to study some boostrap 2x2 grid to display them in a grid but that's another story. Each page has 4 posts as I wanted, using /page/2 , page/3 and pages/4 after the custom url display the posts. Thank you for opening my eyes. Commented Sep 29, 2017 at 22:01
  • No problem. It's always best to work with a platform instead of against it, and WP's templating engine is pretty good.
    – Chris Cox
    Commented Sep 29, 2017 at 23:50

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.