admin管理员组

文章数量:1291562

Currently my page titles are handled by WordPress, in a call to wp_head (I have add_theme_support( 'title-tag' ) in functions.php). I'd like to change the page title format for category archives - currently these look like

<Category name> Archives - <Site name>

I'd like them just to be:

<Category name> - <Site name>

Is there a way of achieving this?

Currently my page titles are handled by WordPress, in a call to wp_head (I have add_theme_support( 'title-tag' ) in functions.php). I'd like to change the page title format for category archives - currently these look like

<Category name> Archives - <Site name>

I'd like them just to be:

<Category name> - <Site name>

Is there a way of achieving this?

Share Improve this question asked Sep 22, 2016 at 14:11 toby1kenobitoby1kenobi 2112 silver badges10 bronze badges 0
Add a comment  | 

4 Answers 4

Reset to default 4

If you're using the Yoast SEO plugin (which it looks like you are) then this answer might help you

If you are using yoast SEO plugin then the easiest method is to remove the archive word from "titles & metas-> Taxonomies->category"

find:

%%term_title%% Archives %%page%% %%sep%% %%sitename%% replace it with:

%%term_title%% %%page%% %%sep%% %%sitename%%

Alternatively you could try changing it using the get_the_archive_title filter as explained here

add_filter( 'get_the_archive_title', function ($title) {

if ( is_category() ) {

        $title = single_cat_title( '', false );

    } elseif ( is_tag() ) {

        $title = single_tag_title( '', false );

    } elseif ( is_author() ) {

        $title = '<span class="vcard">' . get_the_author() . '</span>' ;

    }

return $title;

});

You need to use the wp_title hook, try this code it should work for you :

add_filter( 'wp_title', 'my_new_category_title', 10, 2 );

function my_new_category_title($title)
{
    if (is_category() || is_archive()) {
        $title = ' - '.get_bloginfo('name').' '.$title;
    } 
    return $title;
}

If you only want to edit the title tag () and not what is at the top of the page you can do this:

// define the document_title_parts callback 
function filter_document_title_parts( $title ) { 
    if( is_category() ){
        $title[title] = str_replace( 'Archives', '', $title[title] );
    }
    return $title; 
}; 

// add the filter 
add_filter( 'document_title_parts', 'filter_document_title_parts', 10, 1 );

Also, you can change the default Category Title without using SEO plugins using a function by override the default filter single_cat_title as follow:

/**
* Add Custom Category Title
*/
add_filter( 'single_cat_title', 'custom_category_title', 10, 2 );
function custom_category_title( $title ) {
  if ( is_category() || is_archive() ) {
    $title = $title . ' Example';
  }
  return $title;
}

The output of the above code is:

Category Example

本文标签: wp headModify page title format (when using titletag)