wordpress-seo icon indicating copy to clipboard operation
wordpress-seo copied to clipboard

Does't display %%page%% in single page with pagination

Open cawa-93 opened this issue 6 years ago • 14 comments

  • [x] I've read and understood the contribution guidelines.
  • [x] I've searched for any related issues and avoided creating a duplicate issue.

Please give us a description of what happened.

In my template, the main page is a static page. However, this page has a history of degeneration. is_paged() return true However, the plugin does not add to the <title> information about the paging as it does in the archives

Please describe what you expected to happen and why.

The page https://ihappymama.ru/category/soveti/page/2/ Has title "Архивы Полезные советы ~ Страница 2 из 78" Title template for categoryАрхивы %%term_title%% %%page%%

I was expecting the title of the main page to have the same behavior https://ihappymama.ru/page/2/ "Я happy МАМА ~ Страница 2 из 356" — expected <title> "Я happy МАМА" — real <title> Title template for page%%title%% %%page%%

Technical info

  • WordPress version: 4.9.5
  • Yoast SEO version: 7.4.2

cawa-93 avatar May 08 '18 12:05 cawa-93

Sorry for the late reply. I believe you can edit the post/page itself with the snippet editor on the post/page you set as the homepage:

search_appearance

benvaassen avatar Jul 24 '18 07:07 benvaassen

@benvaassen It had no effect. The pattern of our title is - %%title%% %%page%% but the%%page%% does not showing. This same issue is relevant to another theme. It also has a front page with built-in paging

cawa-93 avatar Jul 25 '18 14:07 cawa-93

Rel prev and rel next are also missing on the second page of your homepage, so I'm guessing we aren't picking up that it's a paginated post/page.

The theme might also be overwriting our settings. It shows here there are specific settings for the homepage. Since it's a paid theme, I'm unable to investigate and reproduce this myself. Can you ask the theme developer for his thoughts on this? Or can you provide me with the theme?

benvaassen avatar Jul 26 '18 08:07 benvaassen

@benvaassen How do i send a template for you?

cawa-93 avatar Jul 26 '18 11:07 cawa-93

If this helps: in the theme we add rel prev, rel next and change the rel canonical based on the global $paged.

add_action( 'wpseo_head', 'rel_next_prev' );
function rel_next_prev(){
	if (!is_page_template('page-blog.php')) {
		return;
	}

	global $blog, $paged;

	if ( $paged > 1 ) { 
		echo '<!-- add in wpseo_head hook --><link rel="prev" href="'.get_pagenum_link( $paged - 1 ).'" />'."\n";

	}

	if ( $paged < $blog->max_num_pages ) { 
		echo '<!-- add in wpseo_head hook --><link rel="next" href="'.get_pagenum_link( $paged +1 ).'" />'."\n";
	}

}

add_filter( 'wpseo_canonical', 'yoast_edit_canonical' );
function yoast_edit_canonical( $canonical ) {
	if (!is_page_template('page-blog.php')) {
		return $canonical;
	}
	global $paged;
	return get_pagenum_link( $paged );
}

In my opinion, this indicates that the WordPress recognizes this page as a page of pagination (is_paged() return true). However, the SEO plugin does not handle paging on static pages

cawa-93 avatar Jul 26 '18 12:07 cawa-93

@benvaassen I suspect the problem here:

https://github.com/Yoast/wordpress-seo/blob/9d2ba283c67e3a6fa7e563bb189a3c04bc13aee2/frontend/class-frontend.php#L567-L571

As far as I can judge by looking at the code, If the home static page is being processed, the variables have the following status:

$modified_title // true
empty( $title ) // false
!empty( $title_part ) // false

Because of this, the presence of paging is ignored

cawa-93 avatar Jul 26 '18 12:07 cawa-93

Thanks for the elaborate replies. I've labeled it as needs-investigation so someone from our team can dive into this.

benvaassen avatar Aug 06 '18 06:08 benvaassen

I have the same problem. Are there some ideas?

korzhikk avatar Aug 28 '18 10:08 korzhikk

I have the same issue on a "is_simple_page()", stated on line 439 of class-frontend.php. Removing these lines will add the page number.

` elseif ( $this->frontend_page_type->is_simple_page() ) { $post = get_post( $this->frontend_page_type->get_simple_page_id() ); $title = $this->get_content_title( $post );

		if ( ! is_string( $title ) || '' === $title ) {
			$title_part = $original_title;
		}
	}`

Any news on this issue? Is there a workaround for this?

jeroensijbom avatar Oct 25 '18 08:10 jeroensijbom

Looks like pages use the get_current_post_page_number method in the Pagination_Helper class which only checks for the 'page' query_var and not 'paged'. So if you have any page with a custom query that applies pagination the titles and canonicals will be incorrect.

Ideally the get_current_post_page_number method would check for the 'page' query_var and then check for the 'paged' query var.

Or pages should make use of the get_current_page_number() method instead which appears to only be used for breadcrumbs but not the title or the canonical.

wpexplorer avatar Jan 04 '23 04:01 wpexplorer

This is still an issue. The static page being used for the home page is a single page. Because %%page%% was being ignored, I tried to force it with:

%%title%% %%sep%% Page %%pagenumber%% of %%pagetotal%%

This rendered the Title Page as: Home: Title | Page 1 of 1

/page/2/ Title | Page 2 of 1

So yeah, it still needs to be fixed...

smkrause avatar Mar 08 '24 22:03 smkrause

@smkrause - Here is class I currently use in my Total theme for a fix if you want to check it out:

ps: It fixes the title and also the canonical which may also be incorrect.

class Yoast_SEO_Pagination_Fix {

	/**
	 * Static-only class.
	 */
	private function __construct() {}

	/**
	 * Init.
	 */
	public static function init() {
		add_filter( 'wpseo_canonical', [ self::class, 'canonical' ], 10, 2 );
		add_filter( 'wpseo_replacements', [ self::class, 'wpseo_replacements' ], 10, 2 );
	}

	/**
	 * Returns the correct canonical when a page is paginated.
	 *
	 * @param string                      $canonical    The current canonical.
	 * @param Indexable_Presentation|null $presentation The indexable presentation.
	 *
	 * @return string The correct canonical.
	 */
	public static function canonical( $canonical, $presentation = null ) {
		if ( self::is_enabled() && $page = get_query_var( 'paged' ) ) {
			if ( str_contains( $canonical, trailingslashit( $page ) ) ) {
				return $canonical;
			}
			global $wp_rewrite;
			if ( is_a( $wp_rewrite, 'WP_Rewrite' )
				&& is_callable( [ $wp_rewrite, 'using_permalinks' ] )
				&& $wp_rewrite->using_permalinks()
			) {
				$canonical = trailingslashit( $canonical );
				if ( ! empty( $wp_rewrite->pagination_base ) ) {
					$canonical .= trailingslashit( $wp_rewrite->pagination_base );
				}
				$canonical = user_trailingslashit( $canonical . $page );
			}
		}

		return $canonical;
	}

	/**
	 * Re-adds the pagination var to the Yoast seo replacements to fix bugs with single pagination.
	 *
	 * @api     array   $replacements The replacements.
	 *
	 * @param array $args The object some of the replacement values might come from,
	 *                    could be a post, taxonomy or term.
	 */
	public static function wpseo_replacements( $replacements, $args = [] ) {
		if ( self::is_enabled() && $page = get_query_var( 'paged' ) ) {
			$sep = '-';
			if ( function_exists( 'YoastSEO' )
				&& ! empty( YoastSEO()->helpers )
				&& ! empty( YoastSEO()->helpers->options )
				&& is_callable( [ YoastSEO()->helpers->options, 'get_title_separator' ] )
			) {
				$sep = YoastSEO()->helpers->options->get_title_separator();
			}
			$replacements['%%page%%'] = sprintf( $sep . ' ' . esc_html__( 'Page %s' ), $page );
		}
		return $replacements;
	}

	/**
	 * Checks if the current page needs a pagination fix.
	 *
	 * @return boolean Whether the current page should apply pagination fixes.
	 */
	protected static function is_enabled() {
		global $wp_query;
		if ( is_singular() && is_paged() && empty( $wp_query->max_num_pages ) ) {
			return true;
		}
	}

}

Yoast_SEO_Pagination_Fix::init();

wpexplorer avatar Mar 11 '24 19:03 wpexplorer

@wpexplorer Thank you! Now custom loop on the page shows page numbers in title. Is it possible to add page number to breadcrumbs to?

Slamik avatar Jun 16 '24 13:06 Slamik

@Slamik - Personally I hide the end title from the breadcrumbs because it's basically just duplicate text since your page should have an h1 with the title, no point in adding it to the crumbs. But I'm sure you could hook into the "wpseo_breadcrumb_single_link_info" hook to modify the output of the title.

wpexplorer avatar Jun 17 '24 22:06 wpexplorer