admin管理员组文章数量:1302384
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!
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!
Share Improve this question edited Feb 15, 2015 at 23:25 user781470 asked Feb 14, 2015 at 23:51 user781470user781470 1581 gold badge1 silver badge6 bronze badges 1- If your question has been answered please mark the accepted answer as such. Thank you. – tacudtap Commented Feb 16, 2015 at 1:44
3 Answers
Reset to default 5If 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/Function_Reference/wp_title
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
本文标签: phpPlace page title in header
版权声明:本文标题:php - Place page title in header? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741674773a2391817.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论