Add filter on wp_title not working with Yoast SEO plugin

For SEO purposes, in a specific template that has a list of items with pagination, I need to have different…

Febbraio 22, 2017

For SEO purposes, in a specific template that has a list of items with pagination, I need to have different urls and titles. Urls change using .../page/2/, .../page/3/ and the paging is handled with get_query_var('paged'). But Yoast SEO plugin doesn’t add automatically the number to the title so in the Google Search Console these pages are considered duplicates.
This code in the functions.php fixes the problem, the number “50” in the add_filter call refers to the priority, so this filter runs after the Yoast’s one and do the work!

function modify_title_with_pagination( $title ) {
	if(get_query_var('paged') && get_query_var('paged') > 0) {
		if ( is_page_template( 'your-template.php' )) {
			return $title . " (page ".get_query_var('paged').")";
		}
	}
    return $title;
}
add_filter( 'wp_title', 'modify_title_with_pagination', 50,2 );

Author

PHP expert. Wordpress plugin and theme developer. Father, Maker, Arduino and ESP8266 enthusiast.

Recommended

Modify the language attribute based on category in WordPress

How to modify the language attribute in your Wordpress theme using a specific value

Novembre 7, 2019

Clean up and optimize WordPress head tag

If you need to know how to remove RSS Feed URL’s from page head or how to remove post relational…

Dicembre 11, 2015

How to add rel=”nofollow” to links with preg_replace()

Adding rel="nofollow" to external link is a good SEO practice.

Settembre 22, 2015

WP Gutenberg notes

Collection of notes and thoughts on Wordpress Gutenberg blocks development.

Gennaio 9, 2023

Test page for Bright Links plugin

To test the plugin hover your mouse to a link, or tap the link on mobile device.

Dicembre 4, 2022

Updating a metabox after Gutenberg updates

When press on "Update" refresh a custom metabox under gutenberg editor.