Home > Software design >  How do I set up a default image for posts without WordPress images?
How do I set up a default image for posts without WordPress images?

Time:08-07

There was a problem with my site that caused many pictures of articles to be deleted

The problem is that the sources of the images are in the article, but the image has been deleted as in the image below :

enter image description here I want a way to set a default image for all articles without an image.

Just for reference, I tried a lot of plugins and it didn't work because in my case there is an image of the article, but the image source is deleted

CodePudding user response:

You can try it : First, you need to create an image that you want to use as the default image. Next, you need to upload it to your theme's images folder using a FTP client. Your theme's images folder is located inside /wp-content/themes/yur-theme/ folder. If it doesn't have the images folder, then you need to create it.

Otherwise you also try this out :

The easiest way to set a default featured image is by using the Default Featured Image plugin. This plugin adds a new setting to the WordPress Media page where you can upload a fallback featured image.

First, you’ll need to install and activate the Default Featured Image plugin. If you need help, then please see our step by step guide on how to install a WordPress plugin.

Upon activation, head over to Settings » Media. Once you’ve done that, just click on the ‘Select default featured image’ button. You can now either choose an image from the WordPress media library or upload a new file.

After choosing your fallback, scroll to the bottom of the screen and click on ‘Save Changes.’ WordPress will now use the image that you selected as your default thumbnail for any posts or pages where you don’t provide a featured image.

You can change the default image at any point by repeating the same process above.

If you no longer want to use a default featured image, then simply go to Settings » Media. Then just click on the ‘Don’t use a default featured image button. Don’t forget to click on ‘Save Changes when you’re done.

CodePudding user response:

If the image deleted than you can check image url exists or not and replace image URL. Following link describe it more: https://www.wpbeginner.com/wp-themes/how-to-set-a-default-fallback-image-for-wordpress-post-thumbnails/

function url_exists($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    return ($code == 200); // "status OK"
}

Use above function to check if url image exists or not like following:

if(url_exists($image_url)){
    //echo delete image here
}else{
    //echo default image here
}

<?php
    $imageUrl = get_the_post_thumbnail_url($post->ID,'full');
    $defaultImageUrl = 'default.jpg';
    if(url_exists($imageUrl)){
        $defaultImageUrl = $imageUrl;
    }
?>

<div > <div > <img src="<?=$defaultImageUrl?>"> </div> </div>
  • Related