admin管理员组文章数量:1334132
I am using the advanced custom fields plugin to upload images for an image gallery. I am attempting to retrieve the different sizes of images that are saved in the uploads folder and having problems.
$image = wp_get_attachment_image_src(get_sub_field('image'), 'full');
When I echo out the results of this I get nothing. I know that the images are being saved as different sizes because I can see them in the uploads folder and the get_sub_field('image') is returning the proper image src.
Is there anything else I need to do to get this to work?
I am using the advanced custom fields plugin to upload images for an image gallery. I am attempting to retrieve the different sizes of images that are saved in the uploads folder and having problems.
$image = wp_get_attachment_image_src(get_sub_field('image'), 'full');
When I echo out the results of this I get nothing. I know that the images are being saved as different sizes because I can see them in the uploads folder and the get_sub_field('image') is returning the proper image src.
Is there anything else I need to do to get this to work?
Share Improve this question asked Nov 27, 2011 at 15:56 pfuncpfunc 1371 gold badge1 silver badge5 bronze badges2 Answers
Reset to default 1What's in get_sub_field('image')? wp_get_attachment_image_src() parameter 1 should be the attachment id, not the attachment src, if that's what you have.
I use the following function to get an attachment ID from an image url as as far as I know WP doesn't have a method for this at present.
function get_attachment_id_from_src ($src) {
global $wpdb;
$reg = "/-[0-9]+x[0-9]+?.(jpg|jpeg|png|gif)$/i";
$src1 = preg_replace($reg,'',$src);
if($src1 != $src){
$ext = pathinfo($src, PATHINFO_EXTENSION);
$src = $src1 . '.' .$ext;
}
$query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$src'";
$id = $wpdb->get_var($query);
return $id;
}
I can almost guarantee that you have the ACF field setting to return "Image URL" instead of "Image ID".
This was the case for me anyway. Once I switched the setting (below) to "Image ID", my problem was solved.
本文标签: imageswpgetattachmentimagesrc() with advanced custom fields returning empty
版权声明:本文标题:images - wp_get_attachment_image_src() with advanced custom fields returning empty 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742237842a2438403.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论