wordpress 缩略图_如何为WordPress发布缩略图设置默认的备用图片

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
为什么需要默认的后备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

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

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
方法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

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视频教程。 您也可以在TwitterFacebook上找到我们。

翻译自: https://www.wpbeginner.com/wp-themes/how-to-set-a-default-fallback-image-for-wordpress-post-thumbnails/

wordpress 缩略图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WordPress中,你可以通过添加特色视频略图来为你的文章或页面增添美观的效果。根据引用中提供的教程,可以按照以下步骤来实现: 1. 首先,确保你已经安装并激活了"Featured Video Plus"插件。这个插件可以让你方便地添加特色视频略图。 2. 在编辑文章或页面的页面中,向下滚动到"Featured Video"选项。这个选项位于编辑器下方的"文档"选项卡旁边。 3. 在"Featured Video"选项中,*** 除此之外,如果你想要设置默认的后备图片,可以参考引用中提到的最佳功能图片插件和教程。这些插件可以让你为没有特色视频的文章自动分配一个默认的后备图片。 希望这些信息对你有所帮助!如果你还有其他问题,请随时向我提问。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [wordpress 略图_如何在WordPress中添加精选视频略图](https://blog.csdn.net/cumyupx7788305/article/details/108606727)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [wordpress 略图_如何为WordPress发布略图设置默认备用图片](https://blog.csdn.net/cumohuo9136/article/details/108608811)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值