Reputation: 137
I want the theme to set a thumbnail image for a post if the user did not choose a thumbnail for when they created the post.
I tried looking through the WordPress codex but I could find any "set method".
<div class="standard-featured">
<?php if( has_post_thumbnail() ) { ?>
<?php the_post_thumbnail(); ?>
<?php } else { ?>
<img src="//Set your image default Path here.">
<?php } ?>
</div>
Upvotes: 0
Views: 278
Reputation: 432
If you want to set post thumbnail from post detail page (single.php). Then first of all add image from wordpress media. Copy it's post id from admin panel or database.
Call below function.
$post_media_id= '';/ id of your uploaded image
$current_post_id = get_the_ID();// current post id
set_post_thumbnail($current_post_id , $post_media_id);
Upvotes: 0
Reputation: 955
There are many ways to do it. Here is a cleaner way to do it. The code example below.
<?php if( has_post_thumbnail() ) : ?>
<?php the_post_thumbnail() ?>
<?php else : ?>
// Dafult thumbnail image for post. try to write the exact image location.
<img src="default.jpg" />
<?php endif; ?>
Upvotes: 2
Reputation: 5429
Try it with the function set_post_thumbnail()
.
More information can be found here: https://developer.wordpress.org/reference/functions/set_post_thumbnail/
Upvotes: 0