WordPress.org

Ready to get started?Download WordPress

Forums

Add Page number to Meta Description in WordPress SEO by Yoast (1 post)

  1. maxbuxfiy
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic