Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to use wp_title() to create a heading for my pages, but I'm using a static front page and, while all of the other pages render the title properly, the front page won't.

This is what I'm working with:

<div id="main-content">
    <h1><?php wp_title("", true); ?></h1>
    <?php while( have_posts() ) : the_post() ?>
        <div class="pagecontent">
            <?php the_content(); ?>
        </div>
    <?php endwhile ?>   
</div>

Initially I thought that the front page might be drawing from index.php, so I added the same code snippet in there – but, no such luck, the same thing gets rendered – an empty h1 tag.

What's going on here? I want the title of the page to show up in the h1 tag.

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

wp_title() is for the html title tags in your websites head section.

It's not for outputting a title. Use the_title(), or get_the_title(),

share|improve this answer
    
Thank you! I replaced wp_title() with the_title() and everything looks good now. –  Benjamin Kovach Jul 10 '12 at 23:34
add comment

If you look at the source of wp_title() you will see that there is not output planned for a static front page.

Use the_title() for visual output as @Chris_O suggested. But for the title in the <head> section you have to filter wp_title() and fill it if it is empty.

Sample code (download from GitHub):

// Hook in very late, let the theme fix it first.
add_filter( 'wp_title', 't5_fill_static_front_page_title', 100 );

/**
 * Fill empty front page title if a static page is set.
 *
 * @wp-hook wp_title
 * @param   string $title Existing title
 * @return  string
 */
function t5_fill_static_front_page_title( $title )
{
    // another filter may have fixed this already.
    if ( '' !== $title or ! is_page() or ! is_front_page() )
    {
        return $title;
    }

    $page_id = get_option( 'page_on_front' );
    $page    = get_page( $page_id );

    if ( ! $page or '' === $page->post_title )
    {
        $title = get_option( 'blogname' );
    }
    else
    {
        $title = $page->post_title;
    }

    // We don’t know if there is any output after the title, so we cannot just
    // add the separator. We use an empty space instead.
    return "$title ";
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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