Gorka Molero
Gorka Molero

Reputation: 51

Wordpress: Custom <title> for archive pages

I'm having a little trouble modifying the page title for my archive pages. I have a custom post type for movies, and I can't seem to alter it. Right now it reads "Movies archive". I'd like to change it altogether to something like "Programme".

Any ideas?

Thanks!

Upvotes: 0

Views: 8695

Answers (3)

Delta
Delta

Reputation: 980

There are also other functions which might be interesting: pre_get_document_title and document_title_parts.

Also if you are using an SEO plugin, you most likely need to play with the priority. In m case I'm using The SEO Framework and had to use the following filter to handle the title for my custom taxonomy:

add_filter(
    'the_seo_framework_title_from_generation',
    function ($title, $args) {
        if (null === $args) {
            // We're in the loop.
            $tsf = tsf();
            if ($tsf->query()->is_tax('gallery-category')) {
                $title = single_term_title('', false);
            }
        }
        return $title;
    },
    10,
    2,
);

Using here the special $tsf variable according to the documentation, if you also need to generate a custom breadcrumb, then follow the example in the docs: https://kb.theseoframework.com/kb/filter-reference-for-the-seo-framework/#set-generated-title

Upvotes: 0

Noah
Noah

Reputation: 384

You can add a filter in functions.php like this to override the title, yet preserve the site name and title separator:

function movies_archive_title( $title ) {

    $site_name = get_bloginfo();
    $sep = apply_filters( 'document_title_separator', '|' );
    $sep = str_pad( $sep, 30, " ", STR_PAD_BOTH );

    if(is_post_type_archive('movie')) 
        return 'Programme'.$sep.$site_name;

    return $title;
}

// Raise priority above other plugins/themes that 
// have effect on the title with 900 (the default is 10).
add_filter( 'wp_title', 'movies_archive_title', 900 ); 
add_filter( 'get_the_archive_title', 'movies_archive_title', 900 );

Replace movie with your custom post-type name and Programme with the desired title in the above example.

With some plugins and themes you need to raise the priority with the priority parameter of the add_filter method or you won't see a change.

Upvotes: 1

d79
d79

Reputation: 3848

It depends on the function your template is using to output the title of your custom post type archive.

I guess it's using either wp_title or get_the_archive_title so you can try adding a filter (inside functions.php of your theme):

function movies_archive_title( $title ) {

    if(is_post_type_archive('movie'))
        return 'Programme';

    return $title;
}
add_filter( 'wp_title', 'movies_archive_title' );
add_filter( 'get_the_archive_title', 'movies_archive_title' );

If the page viewed is the movies archive (be sure to replace 'movie' with the exact name of your custom post type name) then it replace the title with Programme.

Upvotes: 1

Related Questions