2

I want to display different titles in category archives than the category title.

I have created a function with if statements for this purpose:

function category_titles_change() {
    if (is_category('name1')) { ?>  
        new title 1
    <?php }
    elseif (is_category('name2')) { ?>  
        new title 2
    <?php }
    elseif (is_category('name3')) { ?>  
        new title 3
    <?php }
}

How can I use this to change the <title></title> shown in the <head></head>

I have tried this:

function head_category_titles_change() {    
 if (is_category()) { ?> 
 <title> 
<?php category_titles_change(); ?> 
 </title> 
<?php }}
add_action( 'wp_head', 'head_category_titles_change' );

But this ads a second <title> to the <head> in addition to the default title. How can I replace the default Title in Category archives with my desired titles?

2 Answers 2

4

Well thank you for this question i did this after you posted this

function category_title( $title ){
if( is_category('name1') ){
    $title = 'name1 title';
}elseif( is_category('name2') ){
    $title = 'name2 title';
}
return $title;
}
add_filter( 'get_the_archive_title', 'category_title' );

Hope this helps

3
  • +1 for the effort, but doesn't work. Doesn't have any effect.
    – IXN
    Commented Apr 26, 2018 at 13:26
  • It woks if I use add_filter( 'pre_get_document_title', 'category_title' );
    – IXN
    Commented Apr 26, 2018 at 14:39
  • It worked in my archive page bro
    – sagar
    Commented Apr 30, 2018 at 5:18
2

You shouldn't use wp_head action - it doesn't give you a chance to modify title that is already printed. But WordPress has special hook for that: wp_title.

The code below should do the trick:

function my_change_category_title( $title, $sep ) {
    if ( is_category('name1') ) {
        return 'Cat 1 title';
    }
    if ( is_category('name2') ) {
        return 'Cat 2 title';
    }
    ...

    return $title;
}
add_filter( 'wp_title', 'my_change_category_title', 10, 2 );

You can also try to use pre_get_document_title hook with the same filter function.

4
  • +1 for the effort, but doesn't work. Doesn't have any effect.
    – IXN
    Commented Apr 26, 2018 at 13:26
  • And does your theme use wp_title at all? Do you use any SEO plugins that may rewrite the title? Commented Apr 26, 2018 at 13:27
  • I don't use any Seo plugins. There is no wp_title in my functions. There is only: add_theme_support( 'title-tag' ); and this code changing the separator: add_filter( 'document_title_separator', 'xxx_title_separator' ); function xxx_title_separator( $sep ) { $sep = ''; return $sep;} Maybe we should use 'document_title_parts' or wp_get_document_title()?
    – IXN
    Commented Apr 26, 2018 at 13:53
  • 1
    It woks if I use add_filter( 'pre_get_document_title', 'my_change_category_title' );
    – IXN
    Commented Apr 26, 2018 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.