wordpress 缩略图
There are always a group of our readers who are trying to graduate the beginner level early and step into WordPress development. Many of them start by hacking their WordPress themes, creating child themes, or writing plugins. This makes them come across puzzling challenges. One such reader recently asked us how to get the post thumbnail URL in WordPress. The goal here is to just get the post thumbnail URL and then use it with their own markup. In this article, we will show you how to get the post thumbnail URL in WordPress.
总是有一群读者试图尽早毕业于初学者,并逐步进入WordPress开发。 他们中的许多人开始于破解他们的WordPress主题 , 创建子主题或编写插件 。 这使他们面临令人困惑的挑战。 一位这样的读者最近问我们如何在WordPress中获取帖子缩略图URL。 这里的目标是仅获取帖子缩略图 URL,然后将其与自己的标记一起使用。 在本文中,我们将向您展示如何在WordPress中获取帖子缩略图URL。
Note: You must know the basics of how WordPress themes work.
注意:您必须了解WordPress主题如何工作的基础知识。
Simply paste this code inside the loop code that you are writing.
只需将此代码粘贴到您正在编写的循环代码中即可。
<?php
$thumb_id = get_post_thumbnail_id();
$thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail-size', true);
echo $thumb_url[0];
?>
Make sure to replace thumbnail-size
to whatever image size you want to get. Default sizes are thumbnail, medium, large, and full. You can also use add and use additional image sizes. If you are using a custom image size then make sure to regenerate thumbnails. You can also set a default fallback image for post thumbnails.
确保将thumbnail-size
替换为您想要的图像尺寸。 默认大小为缩略图,中,大和完整。 您还可以使用添加和使用其他图像尺寸 。 如果使用自定义图像尺寸,请确保重新生成缩略图 。 您还可以为帖子缩略图设置默认的后备图片 。
What this code does is that we first get the post thumbnail ID. Then we use that id to get the image path or URL using wp_get_attachment_image_src
function, which returns an array that you can use with your own code.
该代码的作用是我们首先获取帖子的缩略图ID。 然后,我们使用该ID通过wp_get_attachment_image_src
函数获取图像路径或URL,该函数返回一个数组,您可以将其与自己的代码一起使用。
We hope this article helped you learn how to get post thumbnail URL in WordPress. For feedback and questions, please leave a comment below.
我们希望本文能帮助您学习如何在WordPress中获取帖子缩略图URL。 对于反馈和问题,请在下面发表评论。
翻译自: https://www.wpbeginner.com/wp-themes/how-to-get-the-post-thumbnail-url-in-wordpress/
wordpress 缩略图