So, in case you forgot, in Genesis Featured Images can be added to the blog page by selecting the checkbox "Include the Featured Image?" in Genesis > Theme Settings > Content Archives (admin url: http://domain.com/wp-admin/admin.php?page=genesis#genesis-theme-settings-posts).
So what if you wanted to remove the featured images of a specific category (say category ID 126 [to get the category ID, use a plugin called Simply Show IDs--makes it quite easy!])?
Here is what I did:
- Copied page_blog.php from the genesis directory, which only contains genesis() function call.
- Then added the following function.
[php]
add_action( 'genesis_before_post' , 'wps_no_feature_image' );
add_action( 'genesis_after_post_content' , 'wps_no_feature_image' );
/*
* Remove Featured Image from Small Group Show category on blog page
* @author Travis Smith
*
*/
function wps_no_feature_image() {
global $post;
if ( has_category( 126 , $post ) && 'genesis_before_post' == current_filter() )
remove_action( 'genesis_post_content', 'genesis_do_post_image' );
elseif ( has_category( 126 , $post ) && 'genesis_after_post_content' == current_filter() )
add_action( 'genesis_before_post_content', 'genesis_do_post_image' );
}
[/php]
First and foremost, we cannot simply remove the action and keep going because then it will be unhooked for all categories on that page after. So what's happening here?
Now, the Genesis Featured Image appears on the genesis_post_content hook. So I need to call my function before that hook (or earlier in priority) to remove that action. So I call the function on genesis_before_post which occurs before genesis_post_content. Then after I pass genesis_post_content hook, I need to add the action back for the next post.
So, if I am illustrating this, it's as though I am walking down the hallway approaching a hook with a hat that I was going to put on my head. Before I get to the hook, my brother takes the hat, and after I pass the hook, my brother puts the hat back on the hook for the next person.
Ali Hussain says
Hi, tahnks for the article
How can i use it in a reverse way, i.e. featured images only for a certain category
Jon Loomer says
Hey, Travis. How would I remove the “Filed Under” and “Tagged With” items in the content archives?
Travis Smith says
Hi Jon,
That’s called post meta data. So something like this should help: https://wpsmith.net/2011/genesis/remove-or-change-post-info-or-post-meta/
Bojan says
Is it possible to change size for FEATURED IMAGE OF A SPECIFIC CATEGORY ???
Travis Smith says
Hello Bojan,
Yes, you would need to use the genesis_pre_get_image filter to accomplish this.
Thanks,
Travis
Dave Thackeray says
Travis this is incredible – so impressed with the knowledge you have on coding.
I have a question. I want category archives to have featured images alongside each post extract in the list, but at the same time I’m trying to configure a portfolio page.
If I set featured image in Theme Settings, the portfolio page displays TWO images – because it’s pulling the featured image as well as the image I have set for it specifically in functions.php.
Is there a way to overcome this – but have featured images display automatically in category archives?
Thank YOU!
Scott says
If I need the featured image on regular archive page (so it is enabled in Genesis settings) but want to disable the featured image on a Custom Post Type page, how would I go about that?
Patrick Brown says
Hi Travis,
Thanks for this tip! I had one bit of feedback, though. This code as it is now only works on the first post of a certain category. After that it doesn’t work. I think that is because when it comes to the next iteration, the action was added back to genesis_before_post_content and not genesis_post_content. So, the next time around, it can’t remove the action again.
Basically, at the end of the function, change ” add_action( ‘genesis_before_post_content’, ‘genesis_do_post_image’ ); ” to ” add_action( ‘genesis_post_content’, ‘genesis_do_post_image’ ); ” and it works perfectly for me!
Sarah says
How would you grab a custom field (image) for the featured image of a specific category? For example, I have a blog of paintings in multiple categories (landscape, people, greeting cards, etc.). I want the painting in the greeting cards category to have a different featured image than the same painting showing up in the people category.
Sarah says
How would you hide featured image for xhtml?