admin管理员组

文章数量:1122846

It seems for some reason, on a theme I'm editing, the 404.php template is being executed every page load, even though what was actually being displayed was perfectly normal, whether it be the index, a single post or page, etc.

I was able to determine this by noticing my widgets were reporting they were being called to render twice upon every page load.

The only remedy was to "return" out of the 404.php file right away, so all the code it contains won't be executed needlessly (or delete it outright).

But now my website has no 404 page.

How can I figure out why this is occurring? Like, a stack trace of events that show what triggers a call to the theme's 404 page?


Edit: Okay, after a lot of head scratching I've determined this is being caused by code similar to this:

$post_img_title_html = '<span class="img-container"><img src="/' . $thumb_name . '" alt="' . $post_title . '" /></span>';

Basically would result in <span><img src=".png" alt="Title" /></span>

BUT...in my test environment, this image does not exist. So it's just a typical "broken image" display. No big deal?

WHY...is this triggering a call to WordPress to completely "render" the theme's 404.php file? Again, it's happening "behind the scenes" as I see no indication of the 404 on the page I'm viewing. But I don't want my website to be doing all this extra rendering/processing of the entire 404 page and it's widgets etc, for nothing?...

Is there some kind of "404 priority" or "sensitivity" setting somewhere. I mean, I don't even know how to put it into words it's so basic.

It does have to do with htaccess. As if I delete the file, the 404's don't occur. But of course, permalinks are broken. I re-save permalinks, pages work again, but every "not found" image results in the theme's 404.php being loaded. Gotta be a way around this?

It seems for some reason, on a theme I'm editing, the 404.php template is being executed every page load, even though what was actually being displayed was perfectly normal, whether it be the index, a single post or page, etc.

I was able to determine this by noticing my widgets were reporting they were being called to render twice upon every page load.

The only remedy was to "return" out of the 404.php file right away, so all the code it contains won't be executed needlessly (or delete it outright).

But now my website has no 404 page.

How can I figure out why this is occurring? Like, a stack trace of events that show what triggers a call to the theme's 404 page?


Edit: Okay, after a lot of head scratching I've determined this is being caused by code similar to this:

$post_img_title_html = '<span class="img-container"><img src="http://example.com/images/animationspotlight/' . $thumb_name . '" alt="' . $post_title . '" /></span>';

Basically would result in <span><img src="http://example.com/image.png" alt="Title" /></span>

BUT...in my test environment, this image does not exist. So it's just a typical "broken image" display. No big deal?

WHY...is this triggering a call to WordPress to completely "render" the theme's 404.php file? Again, it's happening "behind the scenes" as I see no indication of the 404 on the page I'm viewing. But I don't want my website to be doing all this extra rendering/processing of the entire 404 page and it's widgets etc, for nothing?...

Is there some kind of "404 priority" or "sensitivity" setting somewhere. I mean, I don't even know how to put it into words it's so basic.

It does have to do with htaccess. As if I delete the file, the 404's don't occur. But of course, permalinks are broken. I re-save permalinks, pages work again, but every "not found" image results in the theme's 404.php being loaded. Gotta be a way around this?

Share Improve this question edited Apr 14, 2020 at 16:43 yesbutmaybeno asked Apr 13, 2020 at 12:55 yesbutmaybenoyesbutmaybeno 1655 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I'm not totally on track with the issue because I don't have enough information, or any troubleshooting that you've done beyond what appears to be happening from a user-standpoint, but I'm going to give you my best shot here.

In any situation where there are 404 issues, always try to: (1) Reset the permalinks from your WP Dashboard. (2) Try to reset your .htaccess file.

A quick Google of either of those troubleshooting steps should yield a guide on how to accomplish each.

If neither of those two troubleshooting steps accomplish resolve to the issue you're experiencing, then I recommend you dive a little deeper. To begin troubleshooting this issue further than we have, we are going to need more information so let's try the following:

Step 1.

Enable wp_debug (https://wordpress.org/support/article/debugging-in-wordpress/) so that we can see if there are any blaring PHP issues on the pages you're viewing.

I would also try to find out which template is serving the current page that you're looking at when you see the issue with all widgets and plugins enabled as they are when you notice the problem. This will take the addition of some code on your part. There's a great summary on how to achieve this in the following link:

How do you find out which template page is serving the current page?

Step 2.

Then, I would disable all widgets and plugins; Go back to the problem-pages you identified in Step 1 (above), and see if the issue still exists. If so, identify which templates are serving these pages. (Notice whether there is a difference in the templates serving the problem-pages now that the widgets and plugins are disabled).

Work your way through the code of the template that's been identified in each scenario and hopefully this will assist you in identifying where the problem is.

This is not a total solution, but at least a path to get you started. Add more information about what you've found from the steps above, and I'll be happy to assist further. Good luck, and I hope this helps!

本文标签: 404 errorWordpress Theme39s 404 File Getting Called Every Page Load