This question already has answers here: Turn a URL into an Attachment / Post ID (6 answers) Closed 10 years ago.admin管理员组文章数量:1130349
I have the URL of an image which I know is an attachment. Now, I need to find out the attachment ID.
This is what I've got (thanks to PippinsPlugins):
// 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];
}
This works fine for an URL like this: .png
My problem is, the URL is the URL of a cropped picture, so it is .png
My next problem is right now, I can't simply explode the URL to remove 300x297, since I might have pictures, which are not cropped but might have a file name like 'picture2-dontexplodeme.png'
Basically, I need a fool proof solution haha. If there is anyone out there, who has an easy solution, I would be very happy :)
This question already has answers here: Turn a URL into an Attachment / Post ID (6 answers) Closed 10 years ago.I have the URL of an image which I know is an attachment. Now, I need to find out the attachment ID.
This is what I've got (thanks to PippinsPlugins):
// 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];
}
This works fine for an URL like this: http://example/wp-content/uploads/2015/03/picture.png
My problem is, the URL is the URL of a cropped picture, so it is http://example/wp-content/uploads/2015/03/picture-300x297.png
My next problem is right now, I can't simply explode the URL to remove 300x297, since I might have pictures, which are not cropped but might have a file name like 'picture2-dontexplodeme.png'
Basically, I need a fool proof solution haha. If there is anyone out there, who has an easy solution, I would be very happy :)
Share Improve this question asked Apr 13, 2015 at 17:06 websupporterwebsupporter 3,0192 gold badges20 silver badges19 bronze badges 4 |1 Answer
Reset to default 2function get_attachment_id_from_src ($image_src) {
global $wpdb;
$query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$image_src'";
$id = $wpdb->get_var($query);
return $id;
}
This question already has answers here:
Turn a URL into an Attachment / Post ID
(6 answers)
Closed 10 years ago.
I have the URL of an image which I know is an attachment. Now, I need to find out the attachment ID.
This is what I've got (thanks to PippinsPlugins):
// 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];
}
This works fine for an URL like this: .png
My problem is, the URL is the URL of a cropped picture, so it is .png
My next problem is right now, I can't simply explode the URL to remove 300x297, since I might have pictures, which are not cropped but might have a file name like 'picture2-dontexplodeme.png'
Basically, I need a fool proof solution haha. If there is anyone out there, who has an easy solution, I would be very happy :)
This question already has answers here: Turn a URL into an Attachment / Post ID (6 answers) Closed 10 years ago.I have the URL of an image which I know is an attachment. Now, I need to find out the attachment ID.
This is what I've got (thanks to PippinsPlugins):
// 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];
}
This works fine for an URL like this: http://example/wp-content/uploads/2015/03/picture.png
My problem is, the URL is the URL of a cropped picture, so it is http://example/wp-content/uploads/2015/03/picture-300x297.png
My next problem is right now, I can't simply explode the URL to remove 300x297, since I might have pictures, which are not cropped but might have a file name like 'picture2-dontexplodeme.png'
Basically, I need a fool proof solution haha. If there is anyone out there, who has an easy solution, I would be very happy :)
Share Improve this question asked Apr 13, 2015 at 17:06 websupporterwebsupporter 3,0192 gold badges20 silver badges19 bronze badges 4-
1
Is doing it from the CSS (easiest client side with jQuery) an option? e.g. images that have been inserted into posts with the Add Media button will typically have a class like
wp-image-123, indicating the attachment ID. If the images are inserted automatically elsewhere in the theme, could you add a CSS class or a data attribute containing the ID? – William Turrell Commented Apr 13, 2015 at 17:49 - @ialocin yes, I would call it a duplicate. Perfect solution, from there I can go on. thanks – websupporter Commented Apr 13, 2015 at 17:51
- My pleasure. I just remembered there is a good answer to a similar question already. Thats why we still need humans, because the system isn't smart enough - yet :) – Nicolai Grossherr Commented Apr 13, 2015 at 17:54
-
1
You can use core function
attachment_url_to_postid()– JakeParis Commented Sep 13, 2022 at 18:13
1 Answer
Reset to default 2function get_attachment_id_from_src ($image_src) {
global $wpdb;
$query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$image_src'";
$id = $wpdb->get_var($query);
return $id;
}
本文标签: imagesGet Attachment ID from URL
版权声明:本文标题:images - Get Attachment ID from URL 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749238421a2337671.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


wp-image-123, indicating the attachment ID. If the images are inserted automatically elsewhere in the theme, could you add a CSS class or a data attribute containing the ID? – William Turrell Commented Apr 13, 2015 at 17:49attachment_url_to_postid()– JakeParis Commented Sep 13, 2022 at 18:13