7

I'm building a child theme and it currently has a very simple <head> section in header.php:

<head>
  <meta charset="<?php bloginfo( 'charset' ); ?>">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title><?php wp_title( '|', true, 'right' ); ?></title>
  <link rel="profile" href="http://gmpg.org/xfn/11">
  <link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>">
  <!--[if lt IE 9]>
  <script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/js/html5shiv.min.js"></script>
  <![endif]-->

  <?php wp_head(); ?>
</head>

I'm guessing wp_head() is responsible for the second <title> element (it appears just there in the final HTML), but other things I've read say this is impossible.

Should I be removing the <title> from my header.php, or should I be adding something to my functions to remove the title from wp_head() (eg. remove_action('wp_head', 'title') ?

Or should I be doing something else altogether?

7
  • I just noticed that this issue also occurs in the original (parent) theme. Huh! Commented Mar 22, 2015 at 18:06
  • 3
    Are you using Wordpress 4.1 and a theme that is written for 4.1. If so, then you should have this line somewhere in functions.php, add_theme_support( 'title-tag' );. This should explain the two title tags. This feature was introduced in 4.1 and automatically add the wp_title() tag in the header. If so, simply copy your parent theme header.php to your child theme and delete the wp_title() tag in the header.php Commented Mar 22, 2015 at 18:19
  • My pleasure, glad it worked. Enjoy Commented Mar 22, 2015 at 18:39
  • 3
    @PieterGoosen I think your comment should be made into an answer ;-)
    – birgire
    Commented Mar 22, 2015 at 20:02
  • @birgire done, thanks for the suggestion. Also extended a little bit :-) Commented Mar 23, 2015 at 4:27

2 Answers 2

10

The two title tags can be explained as that you are using a theme that is written for Wordpress4.1 and actually is using 4.1. As from 4.1 you don't need to call wp_title() in the head any more, you can make use of new title_tag theme support tag which automatically adds the wp_title() tag in the header

The parent theme you are using are most probably already doing this. Look in your functions.php for this line of code

add_theme_support( 'title-tag' );

As a solution, copy the parent theme header.php to your child theme and simply remove the wp_title() function from the child theme header.php

Here is also a great function to keep in mind for backwards compatibility and is useful for parent theme developers: (Taken from the codex)

 if ( ! function_exists( '_wp_render_title_tag' ) ) {
    function theme_slug_render_title() 
    {
        ?>
        <title>
            <?php wp_title( '|', true, 'right' ); ?>
        </title>
        <?php
    }
    add_action( 'wp_head', 'theme_slug_render_title' );
}
-3

At function.php file in your theme comment code

//add_theme_support( 'title-tag' );
1
  • 1
    This is completely wrong and will never work Commented Feb 3, 2016 at 10:01

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.