WordPress powers over 20% of the charted internet. We’re talking about 60+ million websites. And most of those WordPress installations are set up wrong. Really wrong.

We have a reputation as a rather dashing band of WordPress developers over here and we love sharing what we’ve learned the hard way so you can do it the easy way. We love WordPress and we forge websites out of molten lava every day for clients using it, but the SEO side of it (like any CMS), leaves much to be desired.

For smaller sites, our SEO specialists tweak them by hand. but recently we had a project that involved thousands of pages. To avoid several days of repetitive labor correcting things page by page, our marketing guys walked across the hall to our developer dudes to create some fast automation.

The quest?

wordpress-shield
Conquering a particularly tricky conundrum involving blog page titles and meta descriptions in WordPress, particularly on categories and archive pages. Here’s how they got there and back again…

The site in question had two major SEO issues that required developer (also known as our coders) involvement – duplicate page titles and missing meta descriptions.

Of the two functions we outlined fixes for, changing the page title is the more vital. Search engines treat duplicate page titles as a serious problem. Missing descriptions are less dire but can have a major impact on clickthrough rates.

These problems typically appear on archive and category pages, but can crop up on normal blog posts as well, especially if you have a high number of articles. You’ll also see issues with your page titles if the titles are over the recommended 70 characters and are automatically truncated.

WordPress’s default setup doesn’t do much for descriptions, so we tackled that first. All it took was a simple function to add the description.

NOTE: This implementation assumes that the indispensable WordPress SEO plugin by Yoast is installed, but it can easily be tweaked if you use another setup for SEO.


function add_description()
{
    global $paged;
    global $post;

    if ( $paged > 1 )
    {
        $page_description = "";

        if ( is_category() ) 
        {
            $page_description = strip_tags(category_description());

            if ( empty($page_description) )
            {
                $page_description = 'Post archive for the ' . single_cat_title('', false) . ' category' ;
            }
        }
        else
        {
            $page_description = get_post_meta( $post->ID, '_yoast_wpseo_metadesc', true );
        }

        $new_page_description = 'Page %s of %s';
        $new_page_description = sprintf($new_page_description,
        $paged, strtolower($page_description));

        if ( strlen($page_description) > 156 )
        {
            $page_description = trim(substr($page_description, 0, 156));
        }

        echo '<meta name="description" content="' . $new_page_description . '" />';
    }
}

All you need to do is add the above function to your functions.php file. If you’re running a standard WordPress theme, you’ll want to add_action( ‘wp_head’, ‘add_description’ ) in your functions file. If you’re running the Genesis Framework, you’ll need to add the action in your page templates for the pages in question.

Once we slayed the Description Orcs, it was time to scale Mount Doom and conquer the much more serious issue of duplicate page titles. We emerged victorious by deploying a very similar strategy to the titles as we did with the descriptions.


add_filter('wpseo_title', 'change_title');
function change_title($title)
{
    if ( is_page_template('blog.php') || is_category() )
    {
        global $paged;
        $curr_page = $paged;

        if ( $curr_page == 0 )
        {
            $curr_page = 1;
        }

        if ( strlen($title) > 60 )
        {
            $title = substr($title, 0, 60);
        }

        $title .= " | Page " . $curr_page;
    }

    if ( strlen($title) > 70 )
    {
        $title = substr($title, 0, 67);
        $title .= '...';
    }

    return $title;
}

Again, this implementation hooks into WordPress SEO functionality, but could easily hook into another SEO plugin’s functionality. It also makes sure the page title doesn’t go beyond the recommended 70 characters. You’re welcome.

An alternative quick fix is to prevent indexing on those pages. But there is some SEO value in having your category pages indexed. It’s not much, but every bit helps.

We hope these time-saving tips come in handy. If you have any of your own shortcuts you’d like to share or if you need help with implementation, don’t hesitate to hit us up.

One Response

  1. Rosario Hauptly
    Jun 29, 2017 - 10:13 PM

    I love it

Leave a Comment