Archived topic
404 page has a post title instead of site title
7 replies · Started by Jim on December 21, 2021
The topic title pretty much says it. If you go to https://forestpathology.org and search for gibberish, for some reason the last post title is the title of the 404 page in the header. I don't have a 404 template in the child theme, just a filter to add some text.
Any idea why that might be happening?
Hi Jim,
I'm not seeing the post title in the 404 page: https://www.screencast.com/t/onxMyNEKrF9B
Do I miss anything?
Let me know :)
Thank you for the reply and especially the screenshot. You didn't miss anything, but the mystery has become deeper. This is the 404 page I'm seeing, even when logged out of the website, with caching turned off, and browser cache cleared:

The 404 page you see doesn't have the custom filtered text, or even the same regular text. Color me puzzled.
A little sleuthing tells me the page you saw comes from no-results.php, while the one I saw comes from content-404.php.
To make things worse, a few hours later I'm seeing the same 404 page you saw.
The one you saw if 404 page, the one I saw was no search result page.
So the issue is on 404 page.
Can you try disable all plugins except GP premium to test?
If it still like that, switch to the parent theme to test.
Let me know :)
Disabling all plugins didn't change the 404 page, but switching to parent theme fixed it. So it's something I've done in the child theme, I would guess most likely in functions.php.
I think I found the culprit; this filtering to set up the main title. Somehow it gives results I didn't anticipate on the 404 page. I'll ponder it.
// don't understand this, but it works:
add_filter( 'generate_site_title_output', 'fp_adjust_site_title' );
function fp_adjust_site_title( $output ) {
$site_title = get_bloginfo( 'name' );
$page_name = get_the_title(); // $post->ID in parens causes error
// alter title for all but front page and Blog (home) page
if( !( is_front_page() ) && !( is_home() ) ) {
$site_title = "{$page_name} | {$site_title}"; }
// don't know why this is needed, but without it the Blog page gets "| Forest Pathology"
if( is_home() ) {
$site_title = "Blog | {$site_title}"; }
return sprintf(
'<%1$s class="main-title" itemprop="headline">
<a href="%2$s" rel="home">
%3$s
</a>
</%1$s>',
( is_front_page() && is_home() ) ? 'h1' : 'p',
esc_url( apply_filters( 'generate_site_title_href', home_url( '/' ) ) ),
$site_title
);
}
Hi Jim,
This part of the code is the culprit.
if( !( is_front_page() ) && !( is_home() ) ) {
$site_title = "{$page_name} | {$site_title}"; }
The condition is too broad because the condition is basically saying "apply this on ALL pages except front page and blog page".
You may have to add !( is_404() ) to exclude 404 page from this.
Great suggestion! All fixed. Thanks to both you and Ying for helping me diagnose and fix this.
No problem. Glad you got it sorted. :D