The code below is from WordPress SEO plugin by Yoast. I am trying to add the page number to the Meta description on paginated posts (to avoid duplication issues with Google).
function metadesc( $echo = true ) {
if ( get_query_var('paged') && get_query_var('paged') > 1 )
return;
global $post, $wp_query, $page;
$options = get_wpseo_options();
$metadesc = '';
if (is_singular()) {
$metadesc = wpseo_get_value('metadesc');
if ($metadesc == '' || !$metadesc) {
if ( isset($options['metadesc-'.$post->post_type]) && $options['metadesc-'.$post->post_type] != '' )
$metadesc = wpseo_replace_vars($options['metadesc-'.$post->post_type], (array) $post );
}
}
$metadesc = apply_filters( 'wpseo_metadesc', trim( $metadesc ) );
if ( $echo ) {
if ( !empty( $metadesc ) )
echo '<meta name="description" content="'.esc_attr( strip_tags( stripslashes( $metadesc ) ) ).''.Page .''.$page.'"/>'."n";
else if ( current_user_can('manage_options') && is_singular() )
echo '<!-- '.__( 'Admin only notice: this page doesn't show a meta description because it doesn't have one, either write it for this page specifically or go into the SEO -> Titles menu and set up a template.', 'wordpress-seo' ).' -->'."n";
} else {
return $metadesc;
}
}
I have added $page as a global variable and can output the page number like so:
<meta name="description" content="Wordpress SeoPage3"/>
I would like the output to omit the page number if on Page0 (i.e. first page) then add spaces and separator (pipe or dash) so it reads
<meta name="description" content="Wordpress Seo Page | 3"/>
Thanks..
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
To add a page number regardless to any plugin … use filters. Do not change the plugin file. You cannot run updates other wise.
Example:
<?php # -*- coding: utf-8 -*-
/**
* Plugin Name: T5 Add page number to title
* Description: Adds <code> | Page $number</code> to the page title.
* License: MIT
* License URI: http://www.opensource.org/licenses/mit-license.php
*/
if ( ! function_exists( 't5_add_page_number' ) )
{
function t5_add_page_number( $s )
{
global $page;
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
! empty ( $page ) && 1 < $page && $paged = $page;
$paged > 1 && $s .= ' | ' . sprintf( __( 'Page: %s' ), $paged );
return $s;
}
add_filter( 'wp_title', 't5_add_page_number', 100, 1 );
add_filter( 'wpseo_metadesc', 't5_add_page_number', 100, 1 );
}
Method 2
Just fixed this. I have posted the solution in WordPress forum where I had initially asked.
http://wordpress.org/support/topic/plugin-wordpress-seo-by-yoast-warning-duplicate-meta-descriptions-and-title-tags-when-paginating?replies=4
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0