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 04 Answers
Reset to default 4If 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)
版权声明:本文标题:wp head - Modify page title format (when using title-tag) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741531751a2383796.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论