Skip to content

Instantly share code, notes, and snippets.

@KajKandler
Last active March 16, 2023 09:13
Show Gist options
  • Save KajKandler/570bcce2d1bcb92ea9f5d20a441020b3 to your computer and use it in GitHub Desktop.
Save KajKandler/570bcce2d1bcb92ea9f5d20a441020b3 to your computer and use it in GitHub Desktop.
Wordpress (Astra theme) Archive pagination with all pages

Context

Wordpress with Astra theme, if you look at your theme, you may find a similar pattern and can adopt the solution.

Problem

Search engines like to discover pages via links, not just by sitemap. Category pages seem the ideal place to link to all pages.

In a blog with categories where some are rather large (= produce 5+ pages in an archive).

Wordpress, by default creates pagination links for 'previous', 'next' and if the current page number is cp, it creates links for cp - 2, cp - 1, ..., cp + 1, cp + 2. where 'previous' and 'next' are identical. For example

Previous page 2 3 ... 9 10 Next Page

Yet, if your category pages have more than 5 pages, one page only links to 4 more pages. So it takes at least one more hop to crawl all pages of a category.

This is especially prevelant, if you have nested categories, because parent catories aggregate all posts of the sub-categories.

Solution

Semantics / Goal

To make all posts in a category linked with a shallow hierarchy, display on every page in the archive, links to all other pages in that category (archive). For example:

Previous page 1 2 3 4 5 6 7 8 9 10 Next Page

Techncially

  • Check if your theme has pagination as a plugable function? The Astra theme I use has it as a pluggable function!
  • Create a child-theme (a best practice regardless)
  • Overwrite the function by implementing it in the modified form in the functions.php of your child-theme.

In the case above I simply copied the pagination function from Astra and added the on line marked

                        'show_all'     => true,	// <== added

Caveats

  • If Astra will update their astra_number_pagination in a future release of the theme, this solution will not adapt.
  • The number of pages can get really large and formatting may not be pretty. Yet, human users rarely visit these archive pages anyway.
/** Overwrite Astra pagination to always show all pages */
/**
* Astra Pagination
*
* @since 1.0.0
* @return void Generate & echo pagination markup.
*/
function astra_number_pagination() {
global $wp_query;
$enabled = apply_filters( 'astra_pagination_enabled', true );
// Don't print empty markup if their is only one page.
if ( $wp_query->max_num_pages < 2 || ! $enabled ) {
return;
}
ob_start();
echo "<div class='ast-pagination'>";
/** @psalm-suppress ArgumentTypeCoercion */ // phpcs:ignore Generic.Commenting.DocComment.MissingShort
the_posts_pagination(
array(
'prev_text' => astra_default_strings( 'string-blog-navigation-previous', false ),
'next_text' => astra_default_strings( 'string-blog-navigation-next', false ),
'taxonomy' => 'category',
'in_same_term' => true,
'show_all' => true, // <== added
)
);
/** @psalm-suppress ArgumentTypeCoercion */ // phpcs:ignore Generic.Commenting.DocComment.MissingShort
echo '</div>';
$output = ob_get_clean();
echo apply_filters( 'astra_pagination_markup', $output ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment