WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. kevinator12
    Member
    Posted 2 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
    Member
    Posted 2 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