This little tidbit is for all you WordPress themers out there. It is important that your theme displays the proper title on the page no matter what settings a user has on their site. As you probably are already aware, a user can go to ‘Settings’ -> ‘Reading’ in the WordPress admin menu and change what pages are used for the front page and the posts page (aka blog page)…
So in the example above, I want to set a static page as my homepage and delegate another page to host my blog (aka posts page). So now that my posts page is no longer the front page, I want to display the page title that the user assigned to that page in my theme. This helps add clarity for users and makes all of my pages have a more consistent appearance. As an example, it could be that I am using the posts page for displaying news items and want to label my posts page ‘News’.
Rather than leaving it up to the user to go in and try to hack your theme to get the page title to appear, you decide you want to display this page title for them automatically.
WordPress provides you with the functionthe_title()
, which works great inside the loop. Problem is, you are using that inside the loop to display the title for all of your blog entries on the page and that won’t do you any good when trying to fetch the page title outside the loop. If you try to use this function for the page title outside the loop, all you get is the title of the first post on the page.
So how do we fetch the page title for our posts page?
There are two ways to do it. First is the easy way:
single_post_title();
The only problem you might encounter with this method is that the function echoes our title immediately. If you want to get the title as a variable for any reason, you would have to use output buffering to do it.
You can use the functionget_the_title()
to get the title of a page as a variable just by providing the ID. If you don’t provide an ID, then the function will try to fetch the ID for the current page. Unfortunately, this function doesn’t detect the current page ID properly in our use case, which is why using functionthe_title()
didn’t work for us earlier: the_title()
is just a wrapper function for get_the_title()
.
Luckily for us, WordPress does store the ID of the page you want to use for the posts page in the database. So we can fetch the title as a variable, like this:
$our_title = get_the_title( get_option('page_for_posts', true) );
This may be more than you ever needed to know about fetching the title for an assigned posts page, but now you know! 😉
I like when people post simple, useful stuff; thank you!
Thank you! Was looking all over for this!
Glad it was useful!
I am aware of getting the title of a specific page, searching for static posts page.found this page and used this function for my website.
thanks
Gracias! Muy bueno, aún para los usuarios en países que no hablan inglés.
You can actually use single_post_title(); as a variable just by specifying false as the second parameter: $our_title = single_post_title(“”, false);
Yes. single_post_title(“”,false) is what’s needed in this situation. The function’s second parameter controls whether to echo it or return it.
https://codex.wordpress.org/Function_Reference/single_post_title
This is exactly what I’m looking for. Unfortunately, I’m a newb at proper coding…. any advice on where exactly to put that code?
Thanks!
Stephen, that code is intended to go into the home.php template file. The home.php template file is what handles the blog, whether it is on the front page of the site or not. This code is meant to be used when the blog is not on the front page. This code would go in your h1 tags, or whatever markup you are using, for the page heading.
I’m SO new at this…I don’t see a home.php file when I look in the theme editor in WP or in my files folder on the hosting site. Would it be called something else? maybe single.php? Thx!!
Your blog page will either be handled by front-page.php, home.php or index.php
That is right! Soooo many other suggestions specify changing index.php. Thank you!
Always glad to help!
Thanks for this information.
I used it like this, and it works:
Is this the correct way?
@Ivo,
Sorry, it looks like your code was stripped. Perhaps you could post it in the form on my contact page and I can update your comment and respond?
WooWW, thanks alloottttttttt.
You should have ended up this post by just giving this single_post_title(); function.
This is exactly what I looked for. Thank you! 🙂
This totally helped me out, thank you! I’m surprised it’s not better documented.
This was exactly what I needed. Thank you for this page. 🙂
Great post! This is exactly what I needed to figure out for my new site!
Exactly what I needed 😀
Thank you very much!
This post, …..
Nailed it!
I can’t thank you enough! This was exactly what I needed.
Wow, this made the job easy. I’m so glad I found this. Thank you!
nyc job. exact solution of my problem thanks…
Nice tips ! The codex is not clear enought on this point !
Thanks for your help 🙂
Always glad to help!
Thanks! I was looking for that tidbit exactly for a theme I’m working on right now.
Thanks…worked great!
Thank you! Helped me a lot!
Hi Micah,
thank you for this one.
Have you (or somebody else) got any idea, why that doesn’t work on my project (http://relaunch.locco.de/news/)?
It is the storefront theme with WP 4.8.3.
Basically, that theme is coded up not to have a title at all on the main blog page. So it isn’t a matter of it not working… it is just a matter of it not being coded to work that way.
Oh, thank you for your kind and prompt reply 🙂
Than I have to implement it in another way. Why should it be easy 😉
how do you get the content for a blog page (similarly to how you grabbed the title)?
having trouble searching for that particular snippet!
Typically, if you’ve assigned a blog page you don’t want to show the content. However, you can use `the_content()` function to show the content for a page in WordPress (blog or otherwise): https://developer.wordpress.org/reference/functions/the_content/
thank you, micah–shout out to Atlanta! got some friends at coca-cola…so cheers! also thanks for the building gutenberg blocks post. will do my best to level up and dive in.
i searched “get title of posts page not each post” and your site was at the top of the search results 🙂
#1