wordpress 缩略图_如何在WordPress中添加多个帖子缩略图/特色图片

在WordPress中,默认主题支持特色图片,但有时需要为帖子添加额外的图片。通过安装并激活'Multiple Post Thumbnails'插件,可以实现这个功能。在编辑帖子时,会出现一个用于添加辅助特色图片的元框。要显示这些图片,需要在主题的post循环中插入代码。此外,可以创建新的缩略图大小,并在模板中调用它们。
摘要由CSDN通过智能技术生成

wordpress 缩略图

Recently one of our readers asked if there was a way to add multiple post thumbnails or featured images in a WordPress post. By default, most WordPress themes come with built-in support for featured images which makes adding post thumbnails really simple. However when working on custom projects, you might need to add a second featured image to a post. In this article we will show you how to add multiple post thumbnails / featured images in WordPress.

最近,我们的一位读者询问是否可以在WordPress帖子中添加多个帖子缩略图或特色图片。 默认情况下,大多数WordPress主题都内置了对特色图片的支持,这使得添加帖子缩略图非常简单。 但是,在处理自定义项目时,您可能需要在帖子中添加第二张特色图片。 在本文中,我们将向您展示如何在WordPress中添加多个帖子缩略图/特色图片。

First thing you need to do is install and activate the Multiple Post Thumbnails plugin. After activating the plugin, you need to add the following code in your theme’s functions.php file. You can add it anywhere in the file as long as you are doing it right. Read our guide on how to paste snippets from the web.

您需要做的第一件事是安装并激活“ 多帖子缩略图”插件。 激活插件后,您需要在主题的functions.php文件中添加以下代码。 只要操作正确,就可以将其添加到文件中的任何位置。 阅读我们的指南, 了解如何从网络粘贴摘要



if (class_exists('MultiPostThumbnails')) {

new MultiPostThumbnails(array(
'label' => 'Secondary Image',
'id' => 'secondary-image',
'post_type' => 'post'
 ) );

 }

Once you add that, you can now add a secondary featured image to your post. All you have to do is Edit an existing post or create a new post. You should see a secondary featured image meta box right after the featured image box in WordPress post edit area.

添加完后,您现在可以在帖子中添加辅助图片。 您所要做的就是编辑现有帖子或创建新帖子。 您应该在WordPress帖子编辑区域中的特色图片框后立即看到一个辅助特色图片元框。

Featured and secondary featured image in post edit area

Even though you can add a secondary featured image in your WordPress admin, it will not display on your site until you configure your theme. All you need to do is add the following code inside your post loop where you want to display it. This could be in your single.php (for single posts), archive.php (only for archive pages), etc.

即使您可以在WordPress管理员中添加辅助功能图片,它也不会显示在您的网站上,除非您配置了主题。 您需要做的就是在您要显示的post循环中添加以下代码。 这可以在您的single.php(用于单个帖子),archive.php(仅用于存档页面)等中。


<?php 

if (class_exists('MultiPostThumbnails')) : 

MultiPostThumbnails::the_post_thumbnail(get_post_type(), 'secondary-image');

endif;

 ?>

Once you do that, you are done. Your theme is now ready to display multiple featured images. This is how it looked like when we tested it:

一旦完成,就完成了。 现在,您的主题已准备好显示多个特色图像。 这是我们测试时的样子:

Adding Multiple Post Thumbnails or Featured Images in WordPress

You can repeat this process to add as many featured images as needed. You can also extend the support to other custom post types instead of just posts.

您可以重复此过程以根据需要添加尽可能多的特色图像。 您还可以将支持扩展到其他自定义帖子类型,而不仅仅是帖子。

添加新的缩略图大小 (Adding New Thumbnail Sizes)

If for some reason you need to change the thumbnail size for the secondary featured image, then you can do so by creating additional image size in WordPress. Don’t forget to regenerate thumbnails or new image sizes. After this you can call the new image size in your multiple post thumbnails code. For example, if you created a new image size with the name secondary-featured-thumbnail you would add this code in your template:

如果出于某些原因需要更改辅助功能图片的缩略图大小,则可以通过在WordPress中创建其他图片大小来进行更改。 不要忘记重新生成缩略图或新的图像尺寸 。 之后,您可以在多个帖子缩略图代码中调用新的图像尺寸。 例如,如果使用名称“ secondary-featured-thumbnail创建了新的图像尺寸,则可以在模板中添加以下代码:


<?php 

if (class_exists('MultiPostThumbnails')) : 

MultiPostThumbnails::the_post_thumbnail(get_post_type(), 'secondary-image', NULL,  'secondary-featured-thumbnail');

endif;

?>

We hope that this article helped you add multiple featured images to your WordPress themes. What are some use cases that you can think of for this plugin? When can you see yourself adding multiple post thumbnails in WordPress? Let us know by leaving a comment below.

我们希望本文能帮助您将多个精选图片添加到WordPress主题中。 您可以想到此插件有哪些用例? 什么时候可以看到自己在WordPress中添加多个帖子缩略图? 让我们在下面留下评论。

翻译自: https://www.wpbeginner.com/plugins/how-to-add-multiple-post-thumbnails-featured-images-in-wordpress/

wordpress 缩略图

在使用 WordPress 个客户开发项目的时候,经常会碰到需要设置多个日志缩略图的需求,当然我们可以通过自定义字段实现,但是这样使用起来不是非常方便,没有默认的设置特色图片那么好用,这时候,我一般使用 Multiple Post Thumbnails 这个 WordPress 插件来解决。 Multiple Post Thumbnails 介绍 Multiple Post Thumbnails 是一个面向 WordPress 开发者的一个插件,它定义了一套在后台增加多个缩略图的机制,以及在前端调用新增缩略图的方法,并且它定义的后台空间支持最新的 WordPress 3.5 图片上传方式,使用起来和默认的特色图片基本一致。 Multiple Post Thumbnails:可以定义多个日志缩略图WordPress 插件 Multiple Post Thumbnails 使用方法 前面说过 Multiple Post Thumbnails 是一个面向开发者的插件,所以需要你对主题进行一定的修改才能使用,下面简单介绍下使用方法: 1. 上传插件并激活。  2. 如果你想给当前日志添加第二章缩略图,在当前主题的 functions.php 添加如下代码: if (class_exists('MultiPostThumbnails')) {     new MultiPostThumbnails(         array(             'label' => 'Secondary Image',             'id' => 'secondary-image',             'post_type' => 'post'         )     ); } 当然你可以通过将 post_type 设置为 page,给静态页面设置第二章缩略图,也给自定义日志类型设置。    3. 最后通过下面代码显示定义的缩略图: <?php if (class_exists('MultiPostThumbnails')) : MultiPostThumbnails::the_post_thumbnail(get_post_type(), 'secondary-image'); endif; ?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值