2

I've been trying to find an explanation of the following code which I'm using to add pagination to a custom category template.

$catpage = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
            $catnum = 3;
            $offset = ($catnum * $catpage) - 3;

In particular, I would like to know what exactly the query variable "paged" refers to and also the meaning of "offset".

4

The following code line:

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

is using the ternary operator ?:

It's a shorthand notation for

if( get_query_var( 'paged' ) )
    $paged = get_query_var('paged' );
else
    $paged = 1;

This can in fact be simplified further, by using the the second input parameter for the default value, namely:

$paged = get_query_var( 'paged', 1 );

See the get_query_var() function definition here.

What are query variables?

Let me quote the Codex:

Query vars define a query for WordPress posts.

When ugly permalinks are enabled, query variables can be seen in the URL. For example, in the URL http://example.com/?p=1 the p query var is set to 1, which will display the single post with an ID of 1.

When pretty permalinks are enabled, URLs don't include query variables. Instead, WordPress transforms the URL into query vars via the Rewrite API, which are used to populate the query.

I see you've added more code to the question:

$catpage = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$catnum = 3;
$offset = ($catnum * $catpage) - 3;

Let's check out the category archive for a category called blue:

When the paged query variable isn't defined, on the first page

http://example.tld/category/blue/

then we set it as 1 and then:

offset = 3 * 1 - 3 = 0

When the paged query variable is 2 (on the second page)

http://example.tld/category/blue/page/2/

then:

offset = 3 * 2 - 3 = 6 - 3 = 3

When the paged query variable is 3 on the third page

http://example.tld/category/blue/page/3/

then:

offset = 3 * 3 - 3 = 9 - 3 = 6

etc

  • That's not exactly plain English but I appreciate the answer. What exactly is meant by "paged"? – jrcollins Aug 25 '16 at 11:46
  • Thank you for the detailed explanation. That definitely helps. What is the "offset" variable actually used for? It's in relation to a get_categories array. – jrcollins Aug 25 '16 at 12:18
  • That depends on your code ;-) It can be used for pagination. If you mean the WP_Query then it supports the offset argument and it's used to set the LIMIT part of the generated SQL query, together with the posts_per_page argument. It also supports the paged argument, that can be used as well. The offset argument overrides the paged argument. Both can be overridden by the nopaging argument. @jrcollins – birgire Aug 25 '16 at 12:29
  • aha, I see you've edit your comment with get_categories(). It also supports the offset argument to handle pagination, together with the number argument, It's used to set the LIMIT part of the generated term query SQL. @jrcollins – birgire Aug 25 '16 at 12:36
  • Ok, thanks. This question is in relation to a problem I'm having adding pagination to a custom category template. I was going to ask another question but maybe I should just edit this one? – jrcollins Aug 25 '16 at 12:43
1

It will return the current page number. If the paged variable is not set, it will return 1. 1 stands for page number 1 in pagination.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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