Skip to content

Add Current Page Number to the Page Title

This snippet appends the current pagination number to the page title when viewing paged results. If multiple pages exist, it displays both the current page and the total number of pages (e.g., "Page 2 of 5"). This isn’t enabled by default in the theme to keep titles clean and avoid altering SEO-optimized title structures unless the site owner explicitly chooses to.

/**
 * Display the current page number next to the page title.
 */
add_filter( 'wpex_title', function( $title ) {
	if ( $title && is_paged() ) {
		$paged = get_query_var( 'paged' ) ?: get_query_var( 'page' ) ?: 1;
		if ( $paged > 1 ) {
			global $wp_query;
			$max_num_pages = $wp_query->max_num_pages ?? 0;
			if ( $max_num_pages ) {
				$title .= ' - ' . sprintf( esc_html__( 'Page %s of %s' ), $paged, $max_num_pages );
			} else {
				$title .= ' - ' . sprintf( esc_html__( 'Page %s' ), $paged );
			}
		}
	}
	return $title;
}, 20 );
All PHP snippets should be added via child theme's functions.php file or via a plugin.
We recommend Code Snippets (100% Free) or WPCode (sponsored)
Related Snippets
Back To Top