Learn how to make the WordPress Featured Image only display on certain specific posts
Some Genesis themes display the Featured Image on single posts by default, others do not. It’s a pretty easy snippet to throw into functions.php
to display the image on all posts…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'genesis_entry_header', 'featured_post_image', 8 ); | |
/** | |
* Conditionally add the post thumbnail to single post | |
*/ | |
function featured_post_image() { | |
if ( ! is_singular( 'post' ) ) | |
return; | |
if( ! function_exists( 'get_field' ) ) | |
return; | |
if ( get_field( 'show_image_single' ) ) { | |
the_post_thumbnail( 'post-image' ); | |
} | |
} |
That’s great, but sometimes you may not want the Featured Image to show on certain posts. Maybe you’re using a custom full-width template to style a featured post or you just want to display another image at the top of the post.
With a quick Advanced Custom Fields group and a minor edit to the above code, you can add a checkbox to turn off the Featured Image at will.
Add a single true/false field. You’ll see I selected the Default Value as ‘true’. That’s because I want it to display on everything except what I uncheck. I also moved the ‘Position’ to Side so I can have it display above the image meta box.
Now head over to functions.php
(or preferably your custom plugin) and slap in the following code, ensuring 'show_image_single'
matches the field name you chose.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Code to Display Featured Image on top of the post */ | |
add_action( 'genesis_before_entry', 'featured_post_image', 8 ); | |
function featured_post_image() { | |
if ( ! is_singular( 'post' ) ) return; | |
the_post_thumbnail('post-image'); | |
} |
You’re done. Go check out a post and look at your sidebar. If you want to hide the image, uncheck the box.
Well, that was easy. Thanks!
Hi this is what ive been looking for but I cant see the code you’re referring to. Any advice?
Declan
Thanks for the great demo. Worked perfectly. Just wanted to point out that your sample php code is switched. Th top code should be on the bottom, and the bottom code should be on the top. I was racking my brain for awhile figuring out why it wasn’t working, then realized I was copying the wrong code.
Hello Josh! I am searching for a solution to disable or enable featured images on a template basis but I don’t find an answer how I remove it via .
For example I can hide the title with
~~~
//* Remove the entry title (requires HTML5 theme support)
remove_action( ‘genesis_entry_header’, ‘genesis_do_post_title’ );
~~~
Now I want to do the same with featured images. Do you have an idea?
Greetings from Germany, Moritz
Hey Moritz!
It may be different depending on the child theme your using but try:
remove_action( ‘genesis_entry_content’, ‘genesis_do_post_image’, 8 );
Just tried that to no effect with landscape