get_children does not get attached images for SOME of my posts
-
I am having trouble with the get_children function. I am using the example for “Show the first image associated with the post and re-key the array” in the codex: https://codex.www.ads-software.com/Function_Reference/get_children
$args = array( 'numberposts' => 1, 'order'=> 'DESC', 'post_mime_type' => 'image', 'post_parent' => $post->ID, 'post_type' => 'attachment' ); $get_children_array = get_children($args,ARRAY_A); //returns Array ( [$image_ID]... $rekeyed_array = array_values($get_children_array); $child_image = $rekeyed_array[0]; $TheImageID = $child_image['ID']; $TheImageAttribs = wp_get_attachment_image_src( $TheImageID, 'full' ); // returns an array $TheImageURL = $TheImageAttribs[0];
In most cases, $TheImageURL is the url for the full sized image. However, in SOME cases, the $get_children_array is an empty array, resulting in no value for $TheImageID…
So, sometimes it works, and sometimes it doesn’t (NOTE: All the posts I’ve tried have an attached image in the post content area, which was uploaded through MEDIA).
Does anyone know how to fix this?
I’ve tried this solution, but it doesn’t give any results at all (for any of my posts)
https://www.ads-software.com/support/topic/31-get_children-attachment-problem
- The topic ‘get_children does not get attached images for SOME of my posts’ is closed to new replies.