Place page title in header?

Normally, in WordPress, the page title appears in the content area. I'd like to have the page title appear in the header area. It looks like to do that, I'd have to remove it from its current location in the content-page.php and place it in header.php. But content-page.php is called from page.php, which calls the content-page from within a while loop (while ( have_posts() ) : the_post(); ... ) -- so I'd have to move or copy this into the header as well, I think. Seems like a lot of trouble.

Would it make more sense to move part of the header html into the page content, so I don't have to run the while loop more than once?

(As a learning tool, I'm re-creating an existing html site with WordPress, using the _s starter theme.)

--- EDIT ---

Thanks for answers. Very helpful. Here are results of some testing, based on your answers. Given the following code, in header (outside Loop):

wp_title(): ?php wp_title('', true,''); ?br
the_title(): ?php the_title(); ?br
single_post_title(): ?php single_post_title(); ?br
$post-post_name: ?php echo 'page name: ' . $post-post_name; ?br
ispage? : ?php echo (is_page() ? 'yes' : 'no'); ?br
ispage(about-us) : ?php echo (is_page('about-us') ? 'yes' : 'no'); ?br
ispage(About Us) : ?php echo (is_page('About Us') ? 'yes' : 'no'); ?

When viewed from my About Us page, I get:

wp_title() About UsAt The Tabernacle
the_title(): About Us
single_post_title(): About Us
$post-post_name: page name: about-us
ispage? : yes
ispage(about-us) : yes
ispage(About Us) : yes

When viewed from my Home page, I get:

wp_title(): At The Tabernacle My site motto here
the_title(): Home
single_post_title(): Home
$post-post_name: page name: home
ispage? : yes
ispage(about-us) : no
ispage(About Us) : no

And when viewed from a "Hello World" post, I get:

wp_title(): Hello world!At The Tabernacle
the_title(): Hello world!
single_post_title(): Hello world!
$post-post_name: page name: hello-world
ispage? : no
ispage(about-us) : no
ispage(About Us) : no

Conclusion: I can use the_title() or single_post_title() (wp_title returns more text than I want). And I can test is_page(...) in order to display a specific page name when I'm viewing a post.

Thank you!

Topic screen-layout formatting php Wordpress

Category Web


While above mentioned methods are working for the moment, WordPress core developers recommends as follows:

Starting with 4.1 and Twenty Fifteen, the recommended way for themes to display titles is by adding theme support like this: -Konstantin Obenland

You can either add this line in your theme's functions.php file after the after_setup_theme. or

from same page,

Starting with 4.1 and Twenty Fifteen, the recommended way for themes to display titles is by adding theme support like this:

function theme_slug_setup() {
    add_theme_support( 'title-tag' );
}
add_action( 'after_setup_theme', 'theme_slug_setup' );

Support should be added on the after_setup_theme or init action, but no later than that. It does not accept any further arguments.

What this do is, let WordPress choose the page title in header, without using hardcoded tags in header.php file.

your title will be displays as following format.

Page Title - Site Title


If you only need the title you can request it outside the loop easily.

Try and call the_title() in your header. It should work

But you have to be aware that if you don't put a condition, each page of your website will display its title in the header section.

EDIT: the function to call is get_the_title($post->ID) since the_title() doesn't allow you to specify the post id as an argument. You can check on the Wordpress Codex for function allowing you to query information from your post outside the loop.


You need to use wp_title();

If you're trying to use the post title like so:

<head> 
 <title> post title here </title> 
</head>

You would need to add the wp_title(' ', true , ' ');

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

For example: If your post name was Hello World, Hello World would now show up in the tab.

http://codex.wordpress.org/Function_Reference/wp_title

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.