Answer the question
In order to leave comments, you need to log in
Need to replace an image with another one?
public function check_post_is_has_thumbnail($has_thumbnail, $post, $thumbnail_id)
{
if($has_thumbnail == false ) {
return set_post_thubmnail($post, 0);
}
}
Answer the question
In order to leave comments, you need to log in
return set_post_thubmnail( $post, $thumbnail_id );
Of course, all the data $has_thumbnail, $post, $thumbnail_id should fall into this
function
if ( has_post_thumbnail() ) {
$image = get_the_post_thumbnail_url( get_the_ID(), 'large' );
} else {
$image = get_stylesheet_directory_uri() . '/assets/img/default-cover.jpg';
}
An example of checking for thumbnail existence:
<?php
//должно находится внутри цикла
if( has_post_thumbnail() ) {
the_post_thumbnail();
}
else {
echo '<img src="'.get_bloginfo("template_url").'/images/img-default.png" />';
}
?>
if has_post_thumbnail( $post_id ) { }
$media = get_attached_media( 'image', 2018 );
$media = array_shift( $media );
// ссылка на картинку
$image_url = $media->guid;
// выведем картинку в браузере
echo '<img src="'. $image_url .'" />';
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question