Do you want to set a default fallback image for WordPress post thumbnails? Featured images also known as post thumbnails are very useful in engaging users and making your articles more noticeable on social media. In this article, we will show you how to set a default fallback image for WordPress post thumbnails.

您是否要为WordPress帖子缩略图设置默认的备用图片? 精选图片(也称为帖子缩略图)对于吸引用户并使您的文章在社交媒体上更引人注目非常有用。 在本文中,我们将向您展示如何为WordPress帖子缩略图设置默认的后备图片。



为什么需要默认的后备WordPress发布缩略图? (Why You Need a Default Fallback WordPress Post Thumbnail?)

Post thumbnails or featured images is a WordPress theme feature which allows you to associate an image with your blog post or article. Depending on your theme, this image is then used on homepage, archives, or sidebar widgets.

发布缩略图或特色图片是WordPress主题功能,可让您将图片与博客文章或文章相关联。 然后根据您的主题,将此图像用于主页,档案或侧边栏小部件。

Some WordPress themes display post thumbnail and excerpt of an article on the homepage in a grid layout. If you forget to add a post thumbnail for an article, then it will appear without a thumbnail, and your layout will look broken.

一些WordPress主题以网格布局在主页上显示文章的缩略图和摘录。 如果您忘记为文章添加帖子缩略图,那么它将显示为没有缩略图,并且您的布局看起来很破损。



By adding a fallback image, you can set a branded image to be used when no post thumbnail is found. This allows you to make sure that all your articles have a post thumbnail.

通过添加后备图片,您可以设置未找到帖子缩略图时使用的品牌图片。 这使您可以确保所有文章都有帖子缩略图。

Another way to deal with this problem is using the Require Featured Image plugin. It makes it mandatory for all authors to add a featured image to their articles before publishing.

解决此问题的另一种方法是使用Require Featured Image插件。 强制所有作者在发布前在其文章中添加特色图片。

Having said that, let’s take a look at how to easily set a default fallback image for WordPress post thumbnails.


方法1:使用插件为缩略图创建默认的后备图像 (Method 1: Set Default Fallback Image for Post Thumbnails Using Plugin)

This method is easier and recommended for all users.


First thing you need to do is install and activate the Default Featured Image plugin. For more details, see our step by step guide on how to install a WordPress plugin.

您需要做的第一件事是安装并激活默认特色图片插件。 有关更多详细信息,请参阅有关如何安装WordPress插件的分步指南。

Upon activation, you need to visit the Settings » Media page to configure plugin settings.




On this page, you need click on the ‘Select default featured image’ button to upload or select the image you would like to use as your fallback post thumbnail.


Don’t forget to click on the save changes button after selecting your featured image.


You can now visit your website to see it in action. The plugin will automatically start showing your default fallback image as post thumbnail for articles that do not have a featured image set.

现在,您可以访问您的网站以查看其运行情况。 对于未设置特色图片的文章,该插件将自动开始将您的默认后备图片显示为帖子缩略图。

方法2:手动将后备图像添加为发布缩略图 (Method 2: Add Fallback Image as Post Thumbnail Manually)

This method requires you to add code to your WordPress theme files. If you haven’t done this before, then please take a look at our guide on how to copy and paste code in WordPress.

此方法要求您将代码添加到WordPress主题文件中。 如果您以前没有做过,请查看我们的指南, 了解如何在WordPress中复制和粘贴代码 。

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.

首先,您需要创建要用作默认图像的图像。 接下来,您需要使用FTP客户端将其上传到主题的图片文件夹。

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.

主题的图像文件夹位于/wp-content/themes/yur-theme/文件夹中。 如果没有图像文件夹,则需要创建它。

After you have uploaded the image to your website, the next step is to tell WordPress to look for this image when a post doesn’t have its own post thumbnail.


Your WordPress theme displays post thumbnails in various places. You need to look for the_post_thumbnail() function in theme files. Typically, you’ll find it in archive.php, single.php, or content templates.

您的WordPress主题会在各个地方显示帖子缩略图。 您需要在主题文件中查找the_post_thumbnail()函数。 通常,您可以在archive.php,single.php或内容模板中找到它。

Next, you need to add the following code where you want to display post thumbnail.


  1. <?phpif ( has_post_thumbnail() ) {
  2. the_post_thumbnail();
  3. } else { ?>
  4. <img src=“<?php bloginfo(‘template_directory’); ?>/images/default-image.jpg” alt=“<?php the_title(); ?>” />
  5. <?php } ?>

Don’t forget to replace default-image.jpg with your own image file name.


That’s all, you can now visit your website to see it in action.




方法3:使用文章中的第一张图片作为发布缩略图 (Method 3: Use First Image in an Article as Post Thumbnail)

This method also requires you to add code to your WordPress theme files.


First, you need to add this code to your theme’s functions.php file or a site-specific plugin.

首先,您需要将此代码添加到主题的functions.php文件或特定于站点的插件中 。

  1. //function to call first uploaded image in functions file
  2. functionmain_image() {
  3. $files = get_children(‘post_parent=’.get_the_ID().‘&post_type=attachment
  4. &post_mime_type=image&order=desc’);
  5. if($files) :
  6. $keys = array_reverse(array_keys($files));
  7. $j=0;
  8. $num = $keys[$j];
  9. $image=wp_get_attachment_image($num, ‘large’, true);
  10. $imagepieces = explode(‘”‘, $image);
  11. $imagepath = $imagepieces[1];
  12. $main=wp_get_attachment_url($num);
  13. $template=get_template_directory();
  14. $the_title=get_the_title();
  15. print“<img src=’$main‘ alt=’$the_title‘ class=’frame’ />”;
  16. endif;
  17. }

This code simply outputs the first image added to an article. Now we need to display this output in your theme.

此代码仅输出添加到文章的第一个图像。 现在我们需要在您的主题中显示此输出。

To do that, you will need to edit the theme files where post_thumbnail(); function is used. Replace it with the following code.

为此,您将需要在post_thumbnail();中编辑主题文件。 使用功能。 将其替换为以下代码。

  1. <?phpif ( (function_exists(‘has_post_thumbnail’)) && (has_post_thumbnail()) ) {
  2. echo get_the_post_thumbnail($post->ID);
  3. } else {
  4. echo main_image();
  5. } ?>



You can now visit your website to see it in action.


We hope this article helped you set default fallback image for WordPress post thumbnails. You may also want to see best featured image plugins and tutorials for WordPress.

我们希望本文能帮助您设置WordPress帖子缩略图的默认后备图片。 您可能还想查看WordPress的最佳功能图片插件和教程 。

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

收藏 (0) 打赏


点赞 (0)

1. 本站所有资源来源于用户上传和网络,均不允许转载,如有侵权请联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 如发现会员转载本站资源文章,本站有权封禁会员账号!
4. 不得使用于非法商业用途,商用请购买正版,不得违反国家法律。否则后果自负!
5. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
6. 如有链接无法下载、失效或广告,请联系管理员处理! 7. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!

易生阁资源网 技术分享 如何为WordPress发布缩略图设置默认的备用图片 https://www.yishengge.top/2015.html

  • 本站所有源码、素材版权归原作者所有,新手源码提供的源码只能用于学习研究,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用谷歌浏览器。