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?
5 Answers
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.