php - WordPress: using the_title() on posts pageGet the solution ↓↓↓
I have a page called 'News' and have set this to be my posts page in my WP settings.
However, whenever I call
the_title (outside of the loop) it gives me the title from the first blog post, not the page itself.
Is there a way around this?
I've tried using
wp_title(''); but then it gives me the page title + the site title... which isn't what I want either.
You can use
single_post_title() for this. It will output the name of your posts page:
<?php single_post_title(); ?>
This will output the name. If you do not want to output but return it, you can set the second parameter of the function to false: https://developer.wordpress.org/reference/functions/single_post_title/
<?php $pagename = single_post_title( '', false ); ?>
People are also looking for solutions of the problem: call to undefined function str_contains()
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.