1

I have my posts page set as index.php, and on there I have my main heading (as I do on all pages).

I'm having trouble displaying the page's heading however. The page is called 'Blog' in WordPress, and has been specified as the posts page.

If I output the page heading with wp_title('');, I get the title of the page — 'Blog' — but with the site name after it (perhaps due to Yoast SEO plugin).

If I use the_title() then it gives me the title of the most recent blog post, even though I'm calling the function outside of the loop.

So I've had to resort to simple hardcoding <h1>Blog</h1> which is far from ideal.

Is there a way I can pull in the name of the page title dynamically but just the page title on it's own?

2
  • 1
    First off, wp_title() is used to display and hook into the title element of a document. Secondly, you should not be calling the_title() outside of the loop in the first place. Thirdly, please provide the source of index.php. the_title() works properly when used as directed ;-)
    – tacudtap
    Feb 16 '15 at 1:10
  • I can confirm that I encounter the same specific behaviour. I.e. Most recent blog post being used as page title instead of page title. Sep 15 '19 at 21:30
7

Strange. Outside the loop, the_title() should give you the current page name, if you really are on a page, and not viewing a specific post. If it gives post title instead, it may mean that you are somehow inside a loop. But if that were true, wp_title shouldn't show "Blog".

See if other options give the same result:

//the_title();
single_post_title();
echo $post->post_name; // I think this shows the url page name

Also check for is_page(). You might try the is_page('Blog') test.

if (is_page('blog')) {
echo 'Blog';
}
else {
the_title();
}

just to see what happens.

4
  • 1
    sorry for the massively belated response, single_post_title(); seems to work, I'll go with that! ;)
    – user39214
    May 21 '15 at 12:08
  • Why not use the wp_query object? $wp_query->query[pagename]
    – nodws
    May 2 '19 at 23:12
  • single_post_title(); produces the desired result. Sep 15 '19 at 21:29
  • single_post_title() definitely. Jan 30 at 0:37
3

It's correct that the_title() on blog page will return the most recent post's title and hence it can't be used.

When a page is set as a "posts page" from reading settings, WP calls home.php template file (or index.php if home.php doesn't exist) instead of page.php (see: Template Hierarchy).

So, is_page() conditional won't work on blog page because it's not a page anymore.

The only way to output the actual page title is to use single_post_title();

single_post_title() is a part of the general-template.php file which clearly states that

If we're on the blog page and that page is not the homepage or a single page that is designated as the homepage, use the container page's title.

You can review the code and comments here https://core.trac.wordpress.org/browser/tags/4.4/src/wp-includes/general-template.php#L871

1
$our_title = get_the_title( get_option('page_for_posts', true) );
2
  • 2
    Please edit your answer, and add an explanation: why could that solve the problem?
    – fuxia
    Jul 7 '17 at 13:35
  • it works! ha ha
    – Iggy
    Jun 2 '19 at 2:42
0

I know it's been a while since the OP, but I'm hoping this helps others that came here looking for the same thing.

single_post_title() works for pages until you get to blog and category pages (and presumably product & tag pages in woocommerce)

So first simply check if it's a page, otherwise use the wp_title function

if (is_page()) {
    single_post_title();
} else {
    echo rtrim(wp_title('', false), '- ');
}

I did need to remove the site title from my settings, but I was manually adding it on either side of this function anyway.

0

There are two ways to get the post/page title for WordPress.

get_the_title(); and the_title();

How to use get_the_title();

<h1 class="entry-header text-primary fst-italic"><?php echo esc_html( get_the_title() ); ?></h1>

How to use the_title();

the_title('<h1 class="entry-title  text-primary fst-italic">', '</h1>');

What is difference between get_the_title(); and the_title();? the_title(); ill echo its value, but get_the_title(); does not echo (just returns the value).


This is the standard for WordPress functions: the functions that start with the_ will echo what the corresponding function starting with get_ returns. Other functions have a parameter to choose echo or not and others only return values.

Your Answer

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