WordPress wp_title blank on index page

2019-03-09 07:26发布

问题:

I'm new to WordPress and just installed version 3.3.1.

I did some googling regarding this question and found some answers but they were relevant to version 2.7 and were 2-3 years old.

Basically, the wp_title function works fine on every page except my home page where it returns blank and I get no title whatsoever. I could just hard code the title in but I'd rather not do that.

Guilty line of code:

<title><?php wp_title ( '| So Fresh n\' So Clean', true,'right' ); ?></title>

I couldn't find anything regarding this problem happening in 3.3.1 so clearly I've done something wrong.

回答1:

Here's is what I read from Codex:

If you are using a custom homepage with custom loops and stuff, you will have an empty wp_title. Here goes a neat hack to add the description/tagline at the wp_title place on homepage:

<title><?php bloginfo('name'); ?> | <?php is_front_page() ? bloginfo('description') : wp_title(''); ?></title>

So use is_front_page() to get the title on homepage, the way it is suggested in above code.



回答2:

But if you use a static home page, this is the code:

<title><?php bloginfo('name'); ?> &raquo; <?php is_front_page() ? bloginfo('description') : wp_title(''); ?></title>


回答3:

Update for WordPress versions (>= 4.4)

Try this

function some_name(){
    add_theme_support( 'title-tag' );
}

add_action( 'after_setup_theme', 'some_name' );

Do this in functions.php and remove 'title' tag from head...



回答4:

Working off of Amna's answer, I came up with the following code which should display the page title when there is one, followed by the site name.

<?php wp_title(' - ',TRUE,'right'); bloginfo('name'); ?>

Post/Page Outputs: "The Page Title - Site Name"

Home Page Outputs: "Site Name"


Obviously, this can also be swapped to display the site name first.

<?php bloginfo('name'); wp_title(' - '); ?>

Post/Page Outputs: "Site Name - The Page Title"

Home Page Outputs: "Site Name"


This can also be combined with a conditional to display the site description when viewing the home page.

<?php bloginfo('name'); echo ' - '; is_front_page() ? bloginfo('description') : wp_title(''); ?>

Post/Page Outputs: "Site Name - The Page Title"

Home Page Outputs: "Site Name - The Site Description"



回答5:

For google search on wordpress wp_title empty this is the first result. So I thought that I might share the most elegant solution for this.
In functions.php add a filter for wp_title.

function custom_wp_title( $title, $sep ) {
    if ( is_feed() ) {
        return $title;
    }

    global $page, $paged;

    // Add the blog name
    $title .= get_bloginfo( 'name', 'display' );

    // Add the blog description for the home/front page.
    $site_description = get_bloginfo( 'description', 'display' );
    if ( $site_description && ( is_home() || is_front_page() ) ) {
        $title .= " $sep $site_description";
    }

    // Add a page number if necessary:
    if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
        $title .= " $sep " . sprintf( __( 'Page %s', '_s' ), max( $paged, $page ) );
    }

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


回答6:

I use this one and it never failed:

    function pageTitle($echo){
        $title = "";

        global $paged;
        if (function_exists('is_tag') && is_tag()) {        
            $title .= single_tag_title(__("Tag Archive for &quot;" , 'circle'),false); 
            $title .= '&quot; - '; 
        }
        elseif (is_archive()) {
            $title .= wp_title('',true); 
            //$title .= __(' Archive - ' , 'circle');
            $title .= __(' - ' , 'circle');

        }
        elseif (is_search()) {
        $title .= __('Search for &quot;' , 'circle') . esc_html(get_search_query()).'&quot; - '; 
        }
        elseif (!(is_404()) && (is_single()) || (is_page())) {
            $title .= wp_title('',true); 
            $title .= ' - '; 
        }
        elseif (is_404()) {
            $title .= __('Not Found - ' , 'circle'); 
        }
        if (is_home()) {
            $title .= get_bloginfo('name'); 
            $title .= ' - '; 
            $title .= get_bloginfo('description'); 
        }
        else {
            $title .= get_bloginfo('name'); 
        }
        if ($paged>1) {
            $title .= ' - page ' . $paged; 
        }

        if ( !$echo ) return $title;
        echo $title;
    }

Note that there are translation domains in it that you might want to change.



回答7:

The new hack from Codex is as follows:

<title><?php wp_title(''); ?></title>

Then in your "functions.php" from theme file :

add_filter( 'wp_title', 'baw_hack_wp_title_for_home' );
function baw_hack_wp_title_for_home( $title )
{
  if( empty( $title ) && ( is_home() || is_front_page() ) ) {
    return __( 'Home', 'theme_domain' ) . ' | ' . get_bloginfo( 'description' );
  }
  return $title;
}


回答8:

I use this method in my WordPress site

//Meta Header
if ( ! function_exists( 'dima_wp_title' ) ) :
  function dima_wp_title( $title ) {

    if ( is_front_page() ) {
      return get_bloginfo( 'name' ) . ' | ' . get_bloginfo( 'description' );
    } elseif ( is_feed() ) {
      return ' | RSS Feed';
    } else {
      return trim( $title ) . ' | ' . get_bloginfo( 'name' ); 
    }

  }
  add_filter( 'wp_title', 'dima_wp_title' );
endif;


回答9:

Late to the conversation...

But if you want to use the actual title of the page that you are using for the static front page, you can use the following:

if (is_front_page())
{
    $title = single_post_title( '', false );
}

Although, in the actual source for wp_title(), there is the following, specificaly disabling this for the static front page:

if ( is_single() || ( is_home() && ! is_front_page() ) || ( is_page() && ! is_front_page() ) ) {
    $title = single_post_title( '', false );
}

I suspect there is good reason for this. So, proceed with caution.



回答10:

My 2 cents for "misty lake" theme which had no title on home page and added incorrect title on all other pages.

Just removing the following line from header.php solves the issue, since Wordpress now injects the tag by itself:

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

I consulted the following page – https://make.wordpress.org/themes/2015/08/25/title-tag-support-now-required/



回答11:

You could also put something like this inside your title tag:

<?php 
    if (is_front_page()) { ?> 
        Home | <?php bloginfo('description'); 
    } else {
        wp_title('|', 'true', 'right'); bloginfo('description');
    } ?>