【发布时间】:2019-11-30 09:49:09
【问题描述】:
如果图片已经上传到服务器,我想为 WordPress 网站上的不同帖子设置特色图片。
我打算使用以下代码来做到这一点:
// File has already been uploaded
$image_url = 'path/of/image';
if(file_exists($image_url)) {
set_post_thumbnail($post_id, $image_id);
}
问题是我无法弄清楚如何仅从图像 URL 中获取 $image_id,而没有别的。在 WordPress 中可以吗?
我想再次澄清一下,图片已经在媒体库中。
我已经阅读了这个问题:https://wordpress.stackexchange.com/questions/40301/how-do-i-set-a-featured-image-thumbnail-by-image-url-when-using-wp-insert-post,但它没有告诉我如何获取媒体库中已经存在的图像的id。
我需要与wp_get_attachment_url($id) 相反的东西。该函数根据id返回图片路径。
我想根据路径或 URL 获取图像 id。
谢谢。
【问题讨论】:
-
我知道您上传了一张图片并且您知道路径,现在您想创建该图片的缩略图?
-
@HenryStack,第 :)。我想将该图像设置为 WordPress 帖子的特色图像。
标签: wordpress