WordPress – Setting a default 'featured image' for each Catergory
-
Hi there,
I want to set a default ‘Featured Image’ for every category that I have.
In my functions.php file I have the following code:-
/* ---------------FEATURED POST IMAGE------------------------*/ function default_category_featured_image() { global $post; $featured_image_exists = has_post_thumbnail($post->ID); if (!$featured_image_exists) { $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" ); if ($attached_image) { foreach ($attached_image as $attachment_id => $attachment) { set_post_thumbnail($post->ID, $attachment); }} else if ( in_category('2') ) { set_post_thumbnail($post->ID, '112'); } else if ( in_category('3') ) { set_post_thumbnail($post->ID, '115'); } else if ( in_category('4') ) { set_post_thumbnail($post->ID, '113'); } else if ( in_category('8') ) { set_post_thumbnail($post->ID, '114'); } else { set_post_thumbnail($post->ID, '0'); } } } add_action('the_post', 'default_category_featured_image');
Which is working for most of the Categories apart from WordPress. There are 6 posts at the minute that have ‘WordPress’ as the category and they all are using category ID ‘3’, but for some unknown reason, a few of the posts that have ‘WordPress’ as the category have the default featured image set, where as a few don’t have any default image set?
Any idea why this is happening:-
You can see the problem here:-
From the homepage you can see the title ‘How to increase WordPress Memory Limit’ is working, and ‘Web-Tricks Top 10 Best Plugins for WordPress’ is now, but they have the same category ID – Any ideas?
- The topic ‘WordPress – Setting a default 'featured image' for each Catergory’ is closed to new replies.