欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

wordpress 缩略图_如何在WordPress中添加精选图片或发布缩略图

程序员文章站 2022-04-16 20:33:36
...

wordpress 缩略图

Featured images also known as post thumbnails are a popular feature in WordPress themes. Today most WordPress photography themes, restaurant themes, church themes, and other types of themes have built-in support for post thumbnails. In this article we will show you how to add featured images or post thumbnails in WordPress.

特色图片(也称为帖子缩略图)是WordPress主题中的一项流行功能。 如今,大多数WordPress摄影主题餐厅主题教堂主题和其他类型的主题都内置了对帖子缩略图的支持。 在本文中,我们将向您展示如何在WordPress中添加特色图片或发布缩略图。

You might be thinking why are we using featured image and post thumbnail interchangeably. Well, when this feature was first introduced in WordPress 2.9, it was called post thumbnails. However, in the next version it was renamed to featured image.

您可能在想为什么我们要交替使用特色图片和缩略图。 好吧,当此功能在WordPress 2.9中首次引入时,它称为发布缩略图。 但是,在下一版本中,它已重命名为特色图片。

WordPress特色图片入门指南 (Beginners Guide to Featured Images in WordPress)

Featured Images or Post Thumbnails is a theme feature. Most themes such as Genesis and others support featured images by default.

特色图片或张贴缩略图是主题功能。 默认情况下,诸如Genesis之类的大多数主题都支持特色图像。

An easy way to figure out whether your theme supports featured images is by going to the post editor. Simply create a new post and scroll down a little to see if there is a meta box called featured images on the right hand side of the screen.

确定主题是否支持特色图像的一种简单方法是转到帖子编辑器。 只需创建一个新帖子并向下滚动一点,以查看屏幕右侧是否有一个名为“特色图片”的meta框。

wordpress 缩略图_如何在WordPress中添加精选图片或发布缩略图

影片教学 (Video Tutorial)

Subscribe to WPBeginner
订阅WPBeginner

If you don’t like the video or need more instructions, then continue reading.

如果您不喜欢该视频或需要更多说明,请继续阅读。

在WordPress中添加帖子缩略图或特色图片 (Adding Post Thumbnail or Featured Image in WordPress)

To add a featured image in a WordPress post, simply click on “Set Featured Image” link inside the featured image meta box shown in the screenshot above.

要在WordPress帖子中添加特色图片,只需点击上方屏幕快照所示的特色图片元框内的“设置特色图片”链接即可。

This will open the WordPress Media Uploader. You can use that to upload an image from your computer or use an existing image from your media library. Once you select the image, simply click on Set Featured Image button.

这将打开WordPress Media Uploader。 您可以使用它来从计算机上载图像或使用媒体库中的现有图像。 选择图像后,只需单击“设置精选图像”按钮。

wordpress 缩略图_如何在WordPress中添加精选图片或发布缩略图

The image will appear in the Featured Image meta box, like this:

该图像将显示在“特色图像”元框中,如下所示:

wordpress 缩略图_如何在WordPress中添加精选图片或发布缩略图

It is important to note that the image may appear a little bit differently in your theme. It all depends on how your theme handles featured images.

重要的是要注意,图像在主题中的显示可能会有所不同。 这完全取决于您的主题如何处理特色图像。

Some magazine themes use smaller thumbnails along side post summaries on the main page, and a larger version on the single post view.

一些杂志主题在主页的侧面文章摘要中使用较小的缩略图,在单个文章视图中使用较大的缩略图。

Depending on settings defined by your theme developer, your featured image will automatically appear with your posts. However, if you want to change the way your theme handles featured images and post thumbnails then continue reading.

根据主题开发人员定义的设置,您的特色图片将自动与您的帖子一起显示。 但是,如果您想更改主题处理特色图像和发布缩略图的方式,请继续阅读。

Note: Everything below this will require coding knowledge.

注意:以下所有内容都需要编码知识。

WordPress中主题图片和帖子缩略图的主题开发人员指南 (Theme Developers Guide to Featured Image and Post Thumbnails in WordPress)

Even though featured image is a popular feature supported by a large number of themes, it is still possible that you might be using a theme that does not support featured images. In that case, you can add featured image support to your theme. If you are comfortable editing theme files and know your way around a little CSS, then you can do it yourself.

即使特色图像是受大量主题支持的流行功能,您仍可能使用的主题不支持特色图像。 在这种情况下,您可以为主题添加特色图像支持。 如果您舒适地编辑主题文件并且了解一些CSS的用法,那么您可以自己进行。

To add featured image support in a WordPress theme, you need to add this line of code in your theme’s functions.php file:

要在WordPress主题中添加特色图片支持,您需要在主题的functions.php文件中添加以下代码行:


add_theme_support( 'post-thumbnails' );

This code will enable featured image support for posts and pages. You can now go to posts or pages, and you will see featured image option enabled. However, when you set a featured image it will not automatically display in your WordPress theme. To display featured images in your theme, you need to edit your templates and add this line of code where you want to display the featured image:

此代码将为帖子和页面启用特色图像支持。 现在,您可以转到帖子或页面,您将看到启用了特色图片选项。 但是,当您设置特色图片时,它不会自动显示在WordPress主题中。 要在主题中显示特色图像,您需要编辑模板并在要显示特色图像的位置添加以下代码行:


<?php the_post_thumbnail(); ?>

The files you add the above code in will vary based on your theme. You will want to add the code inside your post loop.

您添加上述代码的文件将根据您的主题而有所不同。 您将需要在循环后添加代码。

The above code is the basic function that you need to add featured image support and display featured images in your theme. To set image size for featured images you upload, you need to add this line of code to your functions.php file.

上面的代码是添加主题图像支持并在主题中显示主题图像所需的基本功能。 要为您上传的特色图像设置图像大小,您需要将此代码行添加到functions.php文件中。


set_post_thumbnail_size( 50, 50);

The parameters for set_post_thumbnail_size are in this order: width, height.

set_post_thumbnail_size的参数按以下顺序排列:宽度,高度。

You can also set additional image sizes to use with the_post_thumbnail() function. For example:

您还可以设置其他图像大小以与the_post_thumbnail()函数一起使用。 例如:


// Image size for single posts
add_image_size( 'single-post-thumbnail', 590, 180 );

In this example we have added a new image size called single-post-thumbnail with 590px with and 180px height. To use this image size in our theme, we will still need to add it in the appropriate theme file. Checkout our guide on adding additional image sizes in WordPress for more details.

在此示例中,我们添加了一个新的图像大小,称为“ 单后缩略图” ,高度为590px,高度为180px。 要在主题中使用此图像大小,我们仍然需要将其添加到适当的主题文件中。 有关更多详细信息,请查看有关在WordPress中添加其他图像尺寸的指南。

If you have previously uploaded featured images, but they are still appearing in some other size, then you need to regenerate thumbnails and image sizes for older posts.

如果您以前上传过精选图片,但它们仍以其他尺寸显示,那么您需要为较早的帖子重新生成缩略图和图片尺寸

Below is an example of the featured image function with a specific image size.

以下是具有特定图像尺寸的特色图像功能的示例。


<?php the_post_thumbnail( 'single-post-thumbnail' ); ?>

This is broken down version of the full functionality. You can further extend the functionality of featured images. For example, you can set a default fallback image for post thumbnails, display featured images with captions, or even add multiple post thumbnails or featured images.

这是完整功能的细分版本。 您可以进一步扩展特色图像的功能。 例如,您可以为帖子缩略图设置默认的后备图像 ,显示带有标题的特色图像 ,甚至添加多个帖子缩略图或特色图像

We hope that this article helped you learn how to add featured images or post thumbnails in WordPress. You may also want to check out our list of 14 best featured images plugins and tutorials for WordPress.

我们希望本文能帮助您学习如何在WordPress中添加特色图片或发布缩略图。 您可能还需要查看我们的14个功能最强大的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/beginners-guide/how-to-add-featured-image-or-post-thumbnails-in-wordpress/

wordpress 缩略图