News

WordPress Tip: Echo the current page number

I’ve recently been working on a site with loads of data that all needed to be linked together by archive page number. So, for example, if you are browsing page 4 of “Category A” posts, you need to easily access links to page 4 of “Category B” posts.

In order to dynamically create these page links, set the following variable within the loop in the archive template:

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

Then I spurt it out when creating my links like so:
<a href="http://example.com/category-b/page/<?php echo $pageNumber; ?>">Back to Category B</a>

The $pageNumber variable will automatically get the page number of the category you are currently browsing, which will then be inserted in your link to go to the same page of the other category.

Tags: , ,

Leave a Reply

Preserve code formatting within <code> and <pre> tags

What is 6 + 2 ?
Please leave these two fields as-is:
Please help us combat comment spam by answering this question.