• Resolved taramacg

    (@taramacg)


    Hi, I’m trying to change the way the page title is generated. It seems to currently display as “page or post title | Blog name” – I’m trying to reverse that so that it reads “Blog name | Title”. I’ve tried changing the code in functions.php from `if ( ! function_exists( ‘_wp_render_title_tag’ ) ) :
    function ct_tracks_add_title_tag() {
    ?>
    <title><?php wp_title( ‘ | ‘ ); ?></title>
    <?php
    }
    add_action( ‘wp_head’, ‘ct_tracks_add_title_tag’ );
    endif;`
    to `if ( ! function_exists( ‘_wp_render_title_tag’ ) ) :
    function ct_tracks_add_title_tag() {
    ?>
    <title><?php bloginfo(‘name’); ?><?php wp_title(‘ | ‘, ‘echo’, ‘left’); ?></title>
    <?php
    }
    add_action( ‘wp_head’, ‘ct_tracks_add_title_tag’ );
    endif;`
    But that doesn’t do anything at all. Anyone know how to do this?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Theme Author Ben Sibley

    (@bensibley)

    Hey Tara,

    WordPress recently changed the best practice for the way themes add the title tag, and the function above is the fallback for pre-4.1 installations. If you’re running WordPress 4.1, it won’t have any affect.

    What you’re looking for is the wp_title() filter. You can use that to modify the title that is output.

    Thread Starter taramacg

    (@taramacg)

    Thanks, can’t find the code for that. What file should I be looking in? If possible I’d like to keep the changes in the child theme.

    Theme Author Ben Sibley

    (@bensibley)

    You can attach a function to the wp_title() filter to change the title text like this:

    function my_title_filter( $title ) {
    
      $title = get_bloginfo( 'name' ) . ' | ' . $title;
    
      return $title;
    
    }
    add_filter('wp_title', 'my_title_filter', 99);

    Once you add that function to your child theme, it will run with the rest of your code. By attaching a function to the wp_title hook, WordPress will run that function every time the wp_title hook is called.

    The end result is that every time WordPress generates the text for a title tag, this function will take the current title, and replace it with the blog name followed by a divider followed by the existing title. It will then return that modified title.

    Thread Starter taramacg

    (@taramacg)

    Oh very good thanks. That’s working perfectly for everything but the home page (a static front page) which is displaying “blogname | blogname| blogdescription”.

    Theme Author Ben Sibley

    (@bensibley)

    Yea no problem! To set the title differently on just the homepage, use the following modification of that function:

    function my_title_filter( $title ) {
    
      if( is_front_page() ) {
        $title = $title;
      } else {
        $title = get_bloginfo( 'name' ) . ' | ' . $title;
      }
      return $title;
    
    }
    add_filter('wp_title', 'my_title_filter', 99);

    I’m not sure what you want the title to be on the homepage, so I just left $title set as itself. You can change that whatever you’d like.

    Thread Starter taramacg

    (@taramacg)

    Perfect. Thank you!

    Theme Author Ben Sibley

    (@bensibley)

    You’re welcome ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Tweaking the page title’ is closed to new replies.