![](https://img-blog.csdnimg.cn/img_convert/b674f8b809c84c16cec967fc052b609f.png)
wordpress 缩略图
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帖子缩略图设置默认的后备图片。
![Fallback image for post thumbnails in WordPress Fallback image for post thumbnails in WordPress](https://img-blog.csdnimg.cn/img_convert/153a5e7edf052c6bf2c02866d971e4f9.png)
为什么需要默认的后备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主题以网格布局在主页上显示文章的缩略图和摘录。 如果您忘记为文章添加帖子缩略图,那么它将显示为没有缩略图,并且您的布局看起来很破损。
![Layout broken without post thumbnail Layout broken without post thumbnail](https://img-blog.csdnimg.cn/img_convert/e3de7b0a02a1bbafee6dad317ed3a63b.png)
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.
话虽如此,让我们看一下如何轻松地为WordPress帖子缩略图设置默认的备用图片。
方法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.
激活后,您需要访问设置»媒体页面以配置插件设置。
![Default featured image settings Default featured image settings](https://img-blog.csdnimg.cn/img_convert/7b950f7f90470798c8241f81c2b982e7.png)
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.
将图片上传到网站后,下一步是告诉WordPress当帖子没有自己的帖子缩略图时查找该图片。
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.
接下来,您需要在要显示帖子缩略图的位置添加以下代码。
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} else { ?>
<img src="<?php bloginfo('template_directory'); ?>/images/default-image.jpg" alt="<?php the_title(); ?>" />
<?php } ?>
Don’t forget to replace default-image.jpg with your own image file name.
不要忘记用您自己的图像文件名替换default-image.jpg。
That’s all, you can now visit your website to see it in action.
就是这样,您现在可以访问您的网站以查看实际运行情况。
![Fallback thumbnail Fallback thumbnail](https://img-blog.csdnimg.cn/img_convert/c9ea15e0100118a125e2a582d7da8380.png)
方法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.
此方法还要求您将代码添加到WordPress主题文件中。
First, you need to add this code to your theme’s functions.php file or a site-specific plugin.
首先,您需要将此代码添加到主题的functions.php文件或特定于站点的插件中 。
//function to call first uploaded image in functions file
function main_image() {
$files = get_children('post_parent='.get_the_ID().'&post_type=attachment
&post_mime_type=image&order=desc');
if($files) :
$keys = array_reverse(array_keys($files));
$j=0;
$num = $keys[$j];
$image=wp_get_attachment_image($num, 'large', true);
$imagepieces = explode('"', $image);
$imagepath = $imagepieces[1];
$main=wp_get_attachment_url($num);
$template=get_template_directory();
$the_title=get_the_title();
print "<img src='$main' alt='$the_title' class='frame' />";
endif;
}
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();中编辑主题文件。 使用功能。 将其替换为以下代码。
<?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) {
echo get_the_post_thumbnail($post->ID);
} else {
echo main_image();
} ?>
![Using first image as the post thumbnail in WordPress Using first image as the post thumbnail in WordPress](https://img-blog.csdnimg.cn/img_convert/cd8aeacc55f9330e8c1fa38885f0e735.png)
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.
如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在Twitter和Facebook上找到我们。
wordpress 缩略图