The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

[resolved] get image id or url for a post (2 posts)

  1. kevinator12
    Posted 4 years ago #

    I have a post that has a an image attachment.

    All I would like to do is get the image ID.

    I tried using get_children(), but I don't understand the result at all.
    I have found multiple solutions to show me the image, but what I really want is the image ID, or the image URL, without it the image being shown.

    Is there a simple solution to this?

  2. kevinator12
    Posted 4 years ago #

    Figured it out.

    The code below creates an array called $child_image which can be easily queried with $child_image['ID'] or $child_image['guid']...

    $args = array(
    	'numberposts' => 1,
    	'order'=> 'ASC',
    	'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];

Topic Closed

This topic has been closed to new replies.

About this Topic