WordPress at a glance

is_paged() WP 1.5.0

Checks whether the pagination page is displayed (page type /page/2, /page/3). Conditional tag.

This function works only for archive pages such as categories, tags, author archives, main blog page, dates etc. I.e. works for those pages that can be divided into several. But does not works for post pages that are divided into multiple pages by <!--nextpage--> tag in content.

Notes:

  • Global. WP_Query. $wp_query Global WP_Query instance.
Works based on: WP_Query::is_paged()

No Hooks.

Return

true/false.

Usage

if( is_paged() ){
	// ...
}

Examples

#1 Remove something from the sidebar on the pagination pages

<?php if( !is_paged() ){ ?>

	<!-- A block that will only be shown on the main page, not the pagination page -->

<?php } ?>

#2 Check on what page of the pagination we are

When you need to check a specific pagination page, use get_query_var('paged').

if( get_query_var('paged') == 3 ){ 
	// Will work on the 3rd page of pagination
}

#3 Check whether posts are divided into pages

you can use the shortcode <!--nextpage--> in post content. This code will divide the text of the post into several pages. And, for example, the URL of the second page will look like this: http://wptest.ru/privet-mir/2/.

Example 1

$paged = get_query_var( 'page' );
// $paged = $wp_query->get( 'page' ); // or we can do so...

if( $paged < 2 ){
	// This is the first page or post is not divided into pages
}
else {
	// That's 2,3,4 ... page of the divided post.
}

Example 2

Check if the post is divided into pages...

the_post();

if( !empty($GLOBALS['multipage']) ){
	// the post is divided into pages
}

Example 3

Let's find out how many pages the post has

the_post();

if( !empty($GLOBALS['numpages']) ){
	if( $GLOBALS['numpages'] === 1 ){
		// not devided
	}
	else {
		// the post is divided into pages
		echo "Number of pages {$GLOBALS['numpages']}";
	}
}

Code of is_paged: wp-includes/query.php VER 4.9.8

<?php
function is_paged() {
	global $wp_query;

	if ( ! isset( $wp_query ) ) {
		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
		return false;
	}

	return $wp_query->is_paged();
}

Related Functions

From tag: Conditional tags (page type and request)

More from tag: Conditional tags (all)

More from category: Queries

More from Template Tags: Main Functions

No comments
    Hello, !     Log In . Register