2

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!

  • If your question has been answered please mark the accepted answer as such. Thank you. – tacudtap Feb 16 '15 at 1:44
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.

  • the_title is intended to only be used within the loop. To get the title of a post outside of the loop use get_the_title( $post->ID ) – Rachel Baker Feb 15 '15 at 18:42
  • Yes sorry I confused the two. – mathieuhays Feb 15 '15 at 20:04
  • Actually, I doubled check and the_title() do work outside the loop. In case of the archive templates it will print the title of first article. In case of single.php and page.php it will print the page title. Can be useful in some cases but not recommended by the documentation – mathieuhays Feb 15 '15 at 21:05
  • Very interesting! As I said, I'm new to WP, so nothing is obvious to me... (Including the fact that, apparently, I can't hit enter for a newline within a comment --- guess I'll post a separate entry below. – user781470 Feb 15 '15 at 22:44
4

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

  • Link-only based questions as not good. Please, explain what to do and why it answers the question. And, if possible, post an example. – cybmeta Feb 15 '15 at 9:35
  • Thanks. You answered first but I had to accept the other response since wp_title wouldn't work for me. Still, good to know about wp_title. – user781470 Feb 16 '15 at 18:11
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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