Custom Titles for Multipaged Posts in WordPress

WordPress is a very strong CMS when it comes to search engine optimization (SEO), unfortunately, there is one area where it does not shine: posts with multiple pages. There are two main problems with the multipage posts (also called paged posts) – duplicate titles and canonical URLs.

WordPress allows you to create posts with multiple pages – very handy if you’re creating a site like Learnthat.com which has dozens of very long courses that are split up into multiple pages. WordPress allows you to insert a comment tag, <!–nextpage–>, which will split your post into as many pages as you desire. This is referred to as multipage, paged, or paging, depending on where you review it. It seems not a lot of blogs use this feature, or if they do, they don’t use it very much as the thinking into SEO for multipage posts doesn’t seem very deep.

Page titles are one of the most important elements for on-page search engine optimization and WordPress retains the same title for each page of a post with multiple pages. This is a negative when really optimizing your site if you’re creating large pieces of content. You’d like the ability to set a unique title for every page of a long multipage post, but the native behavior doesn’t allow that. We use All-in-One SEO Pack for our automatic generation of SEO elements on the page and it doesn’t natively support multiple titles within a post.

Canonical URLs are meta tag based links indicating to search engines what the preferred URL is for a page. This is used to reduce duplicate URLs in search engines. As with page titles, the support for canonical URLs doesn’t distinguish between the first page of a multipage post and the rest of the pages. This can have a major negative effect on your search engine indexing as the canonical URL for the first page is used on all subsequent pages of a paged post. We’re not dealing with canonical URLs in this post, we’ll look at those later.

Custom Page Titles on Paged Posts in WordPress

In this free WordPress tutorial, you will learn how to optimize your page titles by providing an optional custom page title per page in a multipage post in WordPress. We’re going to modify the All-in-One SEO plugin to support this custom title addition. The downside to this approach is we’ll need to re-edit the plugin when an update is made, but if we’re lucky, maybe Semperfi will include this functionality in a future release!

Page Title Meta Tag

We’re going to implement this solution so that if we have specified a custom title, it will use that, otherwise it will add “Page x” to the title. We’re going to use a comment tag to specify our custom title:

<!–pagetitle:This is a Custom Page Title–>

This tag will be used after the <!–nextpage–> tag to specific the title of the current page we are on. So, throughout the document, every time there is a <!–nextpage–> tag, it will be followed by a <!–pagetitle:This is the Second Page–>.

Changes to All in One SEO Pack

We’re going to edit All in One SEO Pack to support our new custom titles. We’re going to edit version 1.6.13.1, if you’re using a different version, you may find these functions on different line numbers. On or about line 626, you will find the rewrite_title() function. This function is what handles the title rewriting for a number of the pages on your site. This is the function you’re going to change.

Open the aioseop.class.php file. Scroll down to line 677, it is immediately after this commented out code:

/*
$new_title = str_replace('%blog_title%', $this->internationalize(get_bloginfo('name')), $title_format);
$new_title = str_replace('%blog_description%', $this->internationalize(get_bloginfo('description')), $new_title);
$new_title = str_replace('%post_title%', $title, $new_title);
$new_title = str_replace('%category%', $category, $new_title);
$new_title = str_replace('%category_title%', $category, $new_title);
$new_title = str_replace('%post_author_login%', $authordata->user_login, $new_title);
$new_title = str_replace('%post_author_nicename%', $authordata->user_nicename, $new_title);
$new_title = str_replace('%post_author_firstname%', ucwords($authordata->first_name), $new_title);
$new_title = str_replace('%post_author_lastname%', ucwords($authordata->last_name), $new_title);
*/

After this commented out code, we’re going to insert some new code.

Press enter after the commented code on line 677 and insert this code:

$jrtitle = $title;
global $page, $paged, $posts, $post;
	if ( $paged >= 2 || $page >= 2 ) {
		$jrtitle .= ' Part ' . max( $paged, $page );
}
if ( $paged >= 2 || $page >= 2 ) {
$pagetitlestring = '/<!--pagetitle:(.*?)-->/';
preg_match_all($pagetitlestring,  $posts[0]->post_content, $titlesarray, PREG_PATTERN_ORDER);
$pagetitles = $titlesarray[1];
//print_r($pagetitles);
$currentpg = $page - 2;
$pagetitle = $pagetitles[$currentpg];
}
if ($pagetitle != '') {
$jrtitle = $pagetitle;
}

Change this line (679):

$d_title = array($this->internationalize(get_bloginfo('name')),
$this->internationalize(get_bloginfo('description')),$title,
$category, $category, $authordata->user_login,
$authordata->user_nicename, ucwords($authordata->first_name),
ucwords($authordata->last_name));

to

$d_title = array($this->internationalize(get_bloginfo('name')),
$this->internationalize(get_bloginfo('description')),$jrtitle,
$category, $category, $authordata->user_login,
$authordata->user_nicename, ucwords($authordata->first_name),
ucwords($authordata->last_name));

Save the aioseop.class.php file.

Add Page Titles to Each Page in Your Post

Now, go through your multipage posts and add a page title, in this format:

Here’s the first page content.

<!–nextpage–>
<!–pagetitle:My Second Page–>

<h2>This is My Second Page</h2>

Here’s my second page content.

<!–nextpage–>
<!–pagetitle:My Third Page–>

<h2>Third pages rock</h2>

There’s a few caveats to the way I handled this. Basically, the function looks through your posts and finds all of the pagetitle meta tags and sticks them into an array. Starting on page two, it reads each entry in the array and makes that the page title. You must start on page 2 entering page titles and not skip any pages as you progress, though technically, you don’t need to make custom page titles on every page, but if you start making them on page 2, you can’t skip any pages or the function will place the wrong page title on some pages. This is because it just reads the next page title in the array and doesn’t really know whether or not a specific page title is tied to a specific page.

Perhaps some of the WordPress gurus out there can help fix that problem.

We hope that All-in-One SEO Pack adds support for this in the future, but in the meantime, these simple changes should allow you to support custom titles on each page of your long multipage posts.

Leave a Reply

Your email address will not be published. Required fields are marked *