4

The wp_title-generated <title> in my custom taxonomy archive pages contains the singular taxonomy name with a colon. I can't figure out where this is coming from (or if it's default Wordpress behavior), and I'd like to remove it. For example, in the archive page for the term 'Vanilla' in a taxonomy called 'Flavors', the <title> is

Flavor: Vanilla | My Site Name

What I would like the title to be is simply

Vanilla | My site name

The code in header.php is this:

<title><?php wp_title('|', true, 'right'); ?></title>

There's only one function in functions.php that's hooked into wp_title, and it looks unrelated to the Taxonomy name. I can't figure out where this is coming from or how to remove it.

How can I remove this?

(The answer in How to remove parent taxonomy name from the title generated by wp_title()? is not generalizable to this, and I'm guessing there's a more direct way to do it.)

2 Answers 2

5

Use wp_title filter to control output

function mamaduka_remove_tax_name( $title, $sep, $seplocation ) {
    if ( is_tax() ) {
        $term_title = single_term_title( '', false );

        // Determines position of separator
        if ( 'right' == $seplocation ) {
            $title = $term_title . " $sep ";
        } else {
            $title = " $sep " . $term_title;
        }
    }

    return $title;
}
add_filter( 'wp_title', 'mamaduka_remove_tax_name', 10, 3 );
4
  • This causes the page title to show up VanillaMy Site Name. It's missing the separator. Using add_filter( 'wp_title', 'mamaduka_remove_tax_name', 10, 3 );
    – supertrue
    Sep 21, 2011 at 3:11
  • @supertrue edited my code now it should work properly
    – Mamaduka
    Sep 21, 2011 at 8:17
  • In case of archive pages using single_term_title might be suffice (it was for me at least :) ) Mar 24, 2014 at 17:32
  • Unfortunately, this will overwrite the titles for archive pages, too.
    – imrek
    Jun 10, 2015 at 8:37
0

I'd recommend you use a SEO plugin to take control of all of your titles. WordPress SEO by Yoast is very good.

Or you could add a filter to wp_title to change the output.

<?php 
add_filter( 'wp_title', 'wpse29020_fix_title', 10, 3 );
function wpse29020_fix_title( $title, $sep, $seplocation )
{
    // If this isn't our flavors taxonomy, just return the title
    if( ! is_tax( 'flavors' ) ) return $title;

    // Get the term
    $obj = get_queried_object();

    // Get the terms name
    $name = sanitize_term_field( 'name', $obj->name, $obj->term_id, 'flavors', 'display' );

    // construct the title
    $title = $name . " $sep " . bloginfo( 'name' );
    return $title;
}
2
  • This code changes my titles to My Site NameVanilla | My Site Name. Do you have any idea why this might be happening? (See also my comment on Mamaduka's answer.)
    – supertrue
    Sep 21, 2011 at 3:42
  • Probably something with your theme. Make sure it's just <title><?php wp_title(''); ?></title>. Also, you should follow Mamaduka's answer: more concise and a better use of the WP api(s). Sep 21, 2011 at 13:32

Your Answer

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

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