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?