wordpress 缩略图

编程入门 行业动态 更新时间:2024-10-07 16:21:48

wordpress <a href=https://www.elefans.com/category/jswz/34/1770191.html style=缩略图"/>

wordpress 缩略图

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帖子编辑区域中的特色图片框后立即看到一个辅助特色图片元框。

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:

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

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中添加多个帖子缩略图? 让我们在下面留下评论。

翻译自: /

wordpress 缩略图

更多推荐

wordpress 缩略图

本文发布于:2024-03-06 16:36:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1715813.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缩略图   wordpress

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!