Loading...

Blog

How to Fix a Custom Post Type’s Archive Page Title When Using the All in One SEO Plugin

Custom Post Types have forever changed WordPress from its roots as a blogging platform into a full on Content Management System. Since the first introduction there has always been an issue with the archives, as in they didn’t even exist. Fast forward to today and a multitude of updates and there are still features and benefits sorely lacking, but that’s another issue altogether, for now let’s focus on an issue we ran across while using the All in One SEO plugin. It seems that even support for Custom Post Type Archives is overlooked by the plugin; they give you plenty of support for the single posts just like WordPress but neglect allowing you to set a custom page title structure for the archives. The archives were picking up the default archive structure and outputting %date%, needless to say this doesn’t fly. So if you’ve been looking for a solution without having to hard code it into the header file, check out our simple PHP function solution.

Rewrite Custom Post Type Archive Page Titles with a WordPress PHP Function

Simply add the following PHP Function code to your WordPress functions.php file.

add_filter( 'aioseop_title', 'dd_rewrite_custom_titles' );
function dd_rewrite_custom_titles( $title ) {
    if ( is_post_type_archive() ) {
        $post_type = get_post_type_object( get_post_type() );
        $blog_title = get_bloginfo();
        $title = $post_type->labels->name . " | " . $blog_title;
    }
    return $title;
}

What does this function do?
Lines 1 and 2 are simply setting up the WordPress Function, with the last line closing it out. Line 3 checks to see if it is a Post Type Archive. Line 4 grabs the Post Type; this is set up in a generic way to apply to all of your Post Types. Line 5 is grabbing the site’s main title. Line 6 combines the main title ($blog_title) with the Custom Post Type’s main name ($post_type->labels->name) to create the title string. Finally Line 8 outputs the custom page title with a format that looks like:

Post Type Name | Site Name

This code is configured to output the page title in a format that we needed, your needs may vary so feel free to modify it to suit your needs.

Troubleshooting

If you are having problems getting this tutorial to work please reread the tutorial and try again, if you still cannot get it to work please leave us a comment below and we will respond as soon as possible. Please do not email us with problems regarding this tutorial, only comments will be responded to.

Leave a Comment

0 comments