Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
    
What I truly wonder is why Joost haven't thought of this... Have you searched about the subject in his site and in WordPress forums? –  brasofilo May 13 '12 at 23:27
    
I started out at the WordPress forums 3 months ago. No answer to date. So I figured I had to do it myself. –  MaxBux May 14 '12 at 5:47

2 Answers 2

up vote 2 down vote accepted

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 );
}
share|improve this answer
    
This is even better. Thank you. I will link to this from WordPress forum. –  MaxBux May 16 '12 at 7:42

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.