Community Forums › Forums › Archived Forums › Design Tips and Tricks › Adding a default featured image
Tagged: default featured image, default thumbnail, featured image, genesis featured image, thumb, thumbnail
- This topic has 3 replies, 2 voices, and was last updated 9 years, 10 months ago by Brad West.
-
AuthorPosts
-
February 26, 2015 at 5:31 pm #142445joycegraceParticipant
Hello, I know this was possible before with a code snippet, but it seems to not be working for me anymore. Not sure if anything has changed since HTML5 themes came out? But I could be way off.
Anyway, I am trying to get a default featured image to work with a theme. So let's say someone publishes a post WITHOUT a featured image set, the theme would display a pre-defined image by default. If someone DOES set a featured image using the back end WP editor, then the theme uses the image that the user set, and not the default image.
Does anyone have a code snippet they've tested with an HTML5 theme that they can share?
For the record, I have googled this, and this one doesn't work for me: http://genesissnippets.com/define-default-post-thumbnail/ (yes I used regenerate thumbnails, just in case, to see if that's what was causing the issue).
Nor does this: http://www.wpbeginner.com/wp-themes/how-to-set-a-default-fallback-image-for-wordpress-post-thumbnails/
I'd rather not use this plugin, as it's a bit overkill for what I want to do: https://wordpress.org/plugins/display-featured-image-genesis/
This plugin is just too outdated, and stopped working for me on some sites, so it's also not an option right now: https://wordpress.org/plugins/genesis-featured-images/
Note: I am not trying to re-position or display the featured image in the post, above the post, or anything like that. I just want to set a default image, in case one is not specified, and prefer to do that with a code snippet in the functions.php file.
Thanks super duper much for your help!
Find me at Joyce Grace (http://www.joycegrace.ca)
February 26, 2015 at 10:22 pm #142473Brad WestMemberI've dug around and can't find anything either. It seems something like this would work for you:
if ( has_post_thumbnail() ) { the_post_thumbnail(); } else { set_post_thumbnail( $post, $thumbnail_id ); }
Only I don't know where to hook this. Not sure where Genesis loads the featured image. May at
genesis_init
?add_action( 'genesis_init', 'child_set_featured_image' ); function child_set_featured_image() { if ( has_post_thumbnail() ) { the_post_thumbnail(); } else { set_post_thumbnail( $post, $thumbnail_id ); } }
Or maybe it could be done with a filter:
add_filter( 'genesis_get_image' , 'genesis_get_image_default' );
February 27, 2015 at 9:39 pm #142623joycegraceParticipantWhere would we define the URL for the thumbnail to use as the default?
Find me at Joyce Grace (http://www.joycegrace.ca)
March 1, 2015 at 9:14 pm #142784Brad WestMemberThat's the thing. There's no reason I can think of to set a default image unless you want to call it for display. In the examples above, you'd call it by ID. What I'd do is add an if statement to call the default image when I needed it. Something like this:
if display_featured_image && has_post_thumbnail { the_post_thumbnail(); } else { echo '<div class="featured-image"><img src="images/default.jpg"></div>'; }
Of course this being Genesis, you'll need to add that in a function and hook it somewhere.
I could be more specific if I knew when and where you wanted the images, but this should get you where you need to go.
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.