Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress WordPress Theme Development WordPress Header and Footer Templates Porting existing headers and footers into WordPress

Pavle Lucic
Pavle Lucic
10,801 Points

wp_title vs the_title

What is the difference?

I have notice wp_title shows nothing, while the_title shows the name of the page?

1 Answer

Mathieu HAYS
Mathieu HAYS
9,623 Points

wp_title is meant to be used in your title tag in the head section.

By default it won't display anything on the homepage. Some plugin or even you can change its behaviour by using a filter. (plugin like Yoast SEO or WordPress SEO optimise its value).

the_title is meant to be used in the loop. It refers to the title of your page or blog post. It's meant to be used in h1, h2 etc...

Hope that makes sense

https://codex.wordpress.org/Function_Reference/wp_title https://codex.wordpress.org/Function_Reference/the_title

Pavle Lucic
Pavle Lucic
10,801 Points

Yes, it makes.

So wp_title by itself have no meaning without Seo plugins?

Mathieu HAYS
Mathieu HAYS
9,623 Points

It does have a meaning. You have to use it inside the title tag in the head.

For example a quick and easy solution would be to have the following:

<title>Your blog name ยป <?php wp_title() ?></title>

but if you want to add a SEO plugin afterward it's going to be a bit weird so you might want to do the following instead:

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

then in functions.php (example taken from the doc) :

<?php
function theme_name_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', 'theme_name_wp_title', 10, 2 );