84669인 학습
152542인 학습
20005인 학습
5487인 학습
7821인 학습
359900인 학습
3350인 학습
180660인 학습
48569인 학습
18603인 학습
40936인 학습
1549인 학습
1183인 학습
32909인 학습
我知道文件的路径,并且希望获取附件 ID。
有一个函数 wp_get_attachment_url() 需要 ID 来获取 URL,但我需要它反向(尽管路径不是 URL)
wp_get_attachment_url()
更新:自 wp 4.0.0 以来,有一个新函数可以完成这项工作。我还没有测试过,但它是这样的:
https://developer.wordpress.org/reference/functions/attachment_url_to_postid/
旧答案:到目前为止,我发现的最佳解决方案如下:
https://frankiejarrett.com /2013/05/get-an-attachment-id-by-url-in-wordpress/
我认为这是最好的,原因有两个:
我用了 pippinsplugins.com 的这个很酷的截图
将此函数添加到您的functions.php 文件中
// retrieves the attachment ID from the file URL function pippin_get_image_id($image_url) { global $wpdb; $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url )); return $attachment[0]; }
然后在您的页面或模板中使用此代码来存储/打印/使用 ID:
// set the image url $image_url = 'http://yoursite.com/wp-content/uploads/2011/02/14/image_name.jpg'; // store the image ID in a var $image_id = pippin_get_image_id($image_url); // print the id echo $image_id;
原始帖子在这里:https://pippinsplugins.com/retrieve-attachment-id-from-图片网址/
希望有帮助;) 弗朗西斯
更新:自 wp 4.0.0 以来,有一个新函数可以完成这项工作。我还没有测试过,但它是这样的:
https://developer.wordpress.org/reference/functions/attachment_url_to_postid/
旧答案:到目前为止,我发现的最佳解决方案如下:
https://frankiejarrett.com /2013/05/get-an-attachment-id-by-url-in-wordpress/
我认为这是最好的,原因有两个:
我用了 pippinsplugins.com 的这个很酷的截图
将此函数添加到您的functions.php 文件中
然后在您的页面或模板中使用此代码来存储/打印/使用 ID:
原始帖子在这里:https://pippinsplugins.com/retrieve-attachment-id-from-图片网址/
希望有帮助;) 弗朗西斯