admin管理员组文章数量:1386311
I am using a static frontpage and a page for my blogposts. In my header.php I want to display the title of the selected page of my blogposts.
For example:
<?php
if( is_home() )
echo '<h1>' . get_the_title() . '</h1>';
?><nav>
...
</nav>
But of course get_the_title()
returns the first element of the displayed posts and not of the page itself.
How can I display the title of the assigned home page?
I am using a static frontpage and a page for my blogposts. In my header.php I want to display the title of the selected page of my blogposts.
For example:
<?php
if( is_home() )
echo '<h1>' . get_the_title() . '</h1>';
?><nav>
...
</nav>
But of course get_the_title()
returns the first element of the displayed posts and not of the page itself.
How can I display the title of the assigned home page?
Share Improve this question edited Dec 5, 2015 at 14:26 fuxia♦ 107k39 gold badges255 silver badges459 bronze badges asked Dec 5, 2015 at 11:09 websupporterwebsupporter 3,0192 gold badges20 silver badges19 bronze badges 1- Related: wordpress.stackexchange/questions/204295/… – Jesse Nickles Commented Mar 19, 2023 at 12:29
5 Answers
Reset to default 6You can make use the queried object to return the title of the page used as blogpage
You can use the following: (Require PHP 5.4+)
$title = get_queried_object()->post_title;
var_dump( $title );
You can get the id from options and then echo the title using that id.
// Blog Page
$page_for_posts_id = get_option( 'page_for_posts' );
echo get_the_title($page_for_posts_id);
// Front Page
$frontpage_id = get_option('page_on_front');
echo get_the_title($frontpage_id);
A note for those thinking of using @pieter's solution on index.php - On index.php, you'll want to check that a static Page is set to show the posts in Setting > Reading. The is_home()
conditional does not work on index.php, so you do need to check the option is set:
<?php
// See if "posts page" is set in Settings > Reading
$page_for_posts = get_option( 'page_for_posts' );
if ($page_for_posts) { ?>
<header class="page-header">
<h1 class="page-title" itemprop="headline">
<?php echo get_queried_object()->post_title; ?>
</h1>
</header>
<?php } ?>
This check is important because index.php is also called when "Front Page Displays > Your latest posts" is set. In that case, get_queried_object()->post_title;
will return an object-not-found error.
This works for me
<?php if ( is_home() ) : ?>
<section class="hero">
<div class="container">
<div class="row">
<div class="col-md-12 text-center">
<h1>news, and personal musings</h1>
</div>
</div>
</div>
If it is just about the home page tile, why don't you hard code the title?
<?php if ( is_home() ) : ?>
<h1>Your blog posts page title</h1>
<?php endif; ?>
本文标签: wp queryHow to output the title of the blogs home page
版权声明:本文标题:wp query - How to output the title of the blogs home page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744542270a2611683.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论