Dear god is there an easier way? to get the attachment ID?
-
Right next to my post I would like to have a “About this featured image section” which in short pulls in the data using
wp_get_attachment_metadata
its cake on a page likeimage.php
where the attachment_id is in$_GET
, But i’m trying to do this on my single-post.php page. Below is my working code:global $post; global $wpdb; $query = " SELECT ID FROM $wpdb->posts WHERE post_parent = '$post->ID' AND post_status = 'inherit' AND post_type='attachment' ORDER BY post_date DESC LIMIT 1"; $attachment_id = $wpdb->get_var($query); $url = get_attachment_link($attachment_id); $urlArray = parse_url($url); $queryStringArray = explode('=', $urlArray['query']); $attachment_id = $queryStringArray[1]; // assign some info $meta_array = wp_get_attachment_metadata($attachment_id);
It works it gives me an array of attachment metadata, but I feel like i went the long way around the bush with this one. Any help on using some of the built in functions in wordpress to clean this up?
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Dear god is there an easier way? to get the attachment ID?’ is closed to new replies.