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!

3 Answers
3

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.

Leave a Comment