0

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?

5

You 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 );
1

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);
5
  • Why would you want to make an unnecessary db call to get the page id while you already have everything available in the queried object. Unless I misread the question and the OP needs the know the page name and display it on all pages, then your solution would be the preferable one ;-) – Pieter Goosen Dec 5 '15 at 16:10
  • I would just consider this another 'option' if you will. ;) One that doesn't come w/a php req. – jgraup Dec 5 '15 at 16:13
  • Well, if you are not running atleast PHP5.5, you have serious issues. With upgrades already stopped on PHP 5.5, you should be using PHP 5.6. ;-) – Pieter Goosen Dec 5 '15 at 16:16
  • Hi jgraup, thanks for the effort, but I prefer Pieters answer for the reasons he mentions. But yours would work too. – websupporter Dec 5 '15 at 16:24
  • Jip, I know. One that doesn't come w/a php req. As I said, if you are not at least running 5.6, you are stuffed. Always better to run the newest. Most crap hosts out there are stuck at 5.3 anyways, so I've added the notice for people on crap hosts, hahahaha – Pieter Goosen Dec 5 '15 at 20:38
1

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.

0

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>

-1

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; ?>
4
  • Why would you want to hardcode stuff. This is messy because you would need to change everything manually each time you set another page as blogpage – Pieter Goosen Dec 5 '15 at 16:12
  • I think so too. In my case, I didn't know, what the client wants to call this page later on. In the layouts he sends its called one time "Blog", the other time "Agenda", in order to avoid long talkings and so on, I want it automatic so he can easily change to the name he prefers without asking me. – websupporter Dec 5 '15 at 16:26
  • The blogpage (home()) is fairly standard imho. And it is exactly the same work whether you change it in the page title or the header. However I do understand that a client would prefer to change the title on the page. – IXN Dec 5 '15 at 16:31
  • @IXN The blogpage (home()) is fairly standard imho. Not if you have set a static frontpage and a blogpage, then it is not standard anymore ;-) – Pieter Goosen Dec 6 '15 at 6:15

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.