Skip to content

Functions Pagination

alex_prokopenko edited this page Jan 19, 2018 · 1 revision

If you want to create custom post type archive - then you will find that standard pagination function doesn't work with it. This happen because inside pagination function there is a check for !is_single().

First of all we need to explain that the page is not single with such code:

global $wp_query;
$wp_query->is_single = false;

This is done automatically when you use method archive_query() inside a model.

After than you can use our custom pagination functions:

<?php
/**
 * Return the next posts page link for custom post type loop.
 *
 * @param WP_Query $wp_query Query to use in pagination checks.
 * @param string   $label Content for link text.
 * @param string   $load_more_attr attribute for generating load more link.
 *
 * @return string|void HTML-formatted next posts page link.
 */
function cpt_next_posts_link( WP_Query $wp_query, $label, $load_more_attr = '' );

/**
 * Return the previous posts page link for custom post type loop.
 *
 * @param WP_Query $wp_query Query to use in pagination checks.
 * @param string   $label Optional. Previous page link text.
 *
 * @return string|void HTML-formatted previous page link.
 */
function cpt_prev_posts_link( WP_Query $wp_query, $label = null );

cpt_next_posts_link() has a feature to generate special code for a "Load More" button. This option described in a separate topic: Load More