Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 tasks done
cawa-93 opened this issue May 8, 2018 · 14 comments
Open
2 tasks done

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

cawa-93 opened this issue May 8, 2018 · 14 comments

Comments

@cawa-93
Copy link
Contributor

cawa-93 commented May 8, 2018

  • I've read and understood the contribution guidelines.
  • 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
@benvaassen
Copy link
Contributor

benvaassen commented Jul 24, 2018

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

@cawa-93
Copy link
Contributor Author

cawa-93 commented Jul 25, 2018

@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

@benvaassen
Copy link
Contributor

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?

@cawa-93
Copy link
Contributor Author

cawa-93 commented Jul 26, 2018

@benvaassen How do i send a template for you?

@cawa-93
Copy link
Contributor Author

cawa-93 commented Jul 26, 2018

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
Copy link
Contributor Author

cawa-93 commented Jul 26, 2018

@benvaassen I suspect the problem here:

if ( ( $modified_title && empty( $title ) ) || ! empty( $title_part ) ) {
$title = $this->get_default_title( $separator, $separator_location, $title_part );
}

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

@benvaassen
Copy link
Contributor

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

@korzhikk
Copy link

I have the same problem. Are there some ideas?

@jeroensijbom
Copy link

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?

@wpexplorer
Copy link
Contributor

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.

@smkrause
Copy link

smkrause commented Mar 8, 2024

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...

@wpexplorer
Copy link
Contributor

wpexplorer commented Mar 11, 2024

@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();

@Slamik
Copy link

Slamik commented Jun 16, 2024

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

@wpexplorer
Copy link
Contributor

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants