I recently came across a strange error while trying to add the custom body classes has_featured_image
and no_featured_image
to posts (based on whether they had a featured image or not).
Here’s what I started with:
[gist id=”7411da07fac39bd146bf” file=”add_body_class_Attempt-1_FAIL.php”]
At first, this seemed to work. However, when I navigated to an archive page, such as page 2 of my posts, I got an error:
Catchable fatal error: Argument 1 passed to genesis_custom_body_class() must be an array, null given in ../genesis/lib/structure/layout.php on line 61
How puzzling, I thought. From what I could tell, I was returning the array via return $classes
but obviously something wasn’t right. After some fiddling, I removed the first conditional check, and guess what? It worked…
[gist id=”7411da07fac39bd146bf” file=”add_body_class_Attempt-2_SUCCESS.php”]
However, I didn’t like the idea of giving up my conditional check. I thought to myself, there has to be something I’m missing. Then it hit me.
I wasn’t returning the array after all! At least, not in the initial conditional check. I was only instructing the function to return
, i.e. exit if the post didn’t match the is_single()
conditional, but I wasn’t telling it to return the actual array of $classes
along with it!
As soon I as realized this, I retraced my steps, adding back in the conditional along with return $classes
, thereby allowing the array to be returned regardless if it was modified or not. Problem solved.
[gist id=”7411da07fac39bd146bf” file=”add_body_class_Attempt-3_SUCCESS.php” highlight=”6″ highlight_color=”#09c”]