Filter Hook: Reversing Category Search Order

When CFIX encounters a post with multiple categories that each have images, or, if falling back to parent categories, with multiple possible parent category images, it will use the image from the most recently added category. In short, CFIX will by default use the most recently added image in many cases. If no category images are found, it will cycle through the categories looking for parent categories with images in the same order.

The following filter function (added to your theme functions.php) will reverse the search order, and instead favor the oldest or earliest-set category or parent image:

Shortcode and Functions

Images can also be shown using the shortcode [cfix_featured_image] or the PHP function cfix_featured_image(). To get the featured image URL, use the function cfix_featured_image_url().

Shortcode/PHP function optional arguments:

* ‘size’: ‘thumbnail’, ‘medium’, ‘large’, ‘full’
* ‘class’: class of the image tag
* ‘alt’: alternative text of the image tag
* ‘title’: title of the image tag
* ‘cat_id’: select a specific category id

Shortcode example:

Function example 1:

Function example 2:

When Images Don’t Show Up, Or: Curing Bad Thumbnail Data

If your site has been moved, features posts imported or restored via WP-Import or other tools, including some popular backup utilities, you may have numerous posts with bad thumbnail data that WordPress seems to think have thumbnails or featured images even though none show up, also preventing category image fallbacks from functioning.

If you have this problem and you are either unable or disinclined to try to restore the missing thumbnails, you can add the following function to your theme functions.php file.

The downside of this approach is that, because the bad data is not found and removed until the post has been loaded, the positive effect will not show up until the second time the affected post is loaded.

An alternative is to cure the entire database in one go, using a variation on the above function…

It needs only to run once to fix whatever problems may currently exist. So you can delete it once you’re done, or disable it by commenting out the add_action

…and removing the double forward slash if you ever need to run it again.