Title: simple codex/php coding question: get_children
Last modified: August 19, 2016

---

# simple codex/php coding question: get_children

 *  Resolved [derekbeck](https://wordpress.org/support/users/derekbeck/)
 * (@derekbeck)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/simple-codexphp-coding-question-get_children/)
 * I’m trying to call the WP database to get the links for all images attached to
   a given post. Here’s my code, but it is going into the if statement (no attachments),
   rather than the else statement (with attachments).
 * What am I doing wrong?
 *     ```
       $attachedimages =& get_children('post_type=attachment&post_mime_type=image');
   
       if ( empty($attachedimages) ) {
       	// no attachments here
       	print "<!-- no attached -->";
       } else {
       	foreach ( $attachedimages as $attachment_id => $attachment ) {
       	   	print "\n" . '<meta property="og:image" content="' . wp_get_attachment_image( $attachment_id, 'full' ) . '"/>';
       }
       }
       ```
   
 * Any ideas?

Viewing 9 replies - 1 through 9 (of 9 total)

 *  Thread Starter [derekbeck](https://wordpress.org/support/users/derekbeck/)
 * (@derekbeck)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/simple-codexphp-coding-question-get_children/#post-2053000)
 * I have made some slight progress, and if I use the following code, it properly
   lists the image TITLES (inside comments for testing). But how do I render the
   image permalinks, preferablly to the thumbnail size?
 *     ```
       $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID );
       $attachments = get_posts($args);
       if ($attachments) {
       	foreach ( $attachments as $attachment ) {
       		$image = wp_get_attachment_url( get_attachment_link() );
   
       		print "<!-- " . apply_filters( 'the_title' , $attachment->post_title ) . " -->";
   
       	}
       }
       ```
   
 *  [Michael Fields](https://wordpress.org/support/users/mfields/)
 * (@mfields)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/simple-codexphp-coding-question-get_children/#post-2053005)
 * See:
    [http://codex.wordpress.org/Function_Reference/get_attachment_link](http://codex.wordpress.org/Function_Reference/get_attachment_link)
   [http://codex.wordpress.org/Function_Reference/wp_get_attachment_url](http://codex.wordpress.org/Function_Reference/wp_get_attachment_url)
 *  Thread Starter [derekbeck](https://wordpress.org/support/users/derekbeck/)
 * (@derekbeck)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/simple-codexphp-coding-question-get_children/#post-2053027)
 * I can’t figure it out. I don’t understand the coding sufficiently to figure it
   out it seems.
 * In the first code example above: the result is every image in my database rendered.
   Don’t know why. But I feel the wp_get_attachment_url function would work with
   that code if I understood it better.
 * In the second example code above: I think the $args = array is limiting what 
   data is pulled, but not sure. And I don’t think the wp_get_attachment_url function
   will work with that code.
 * In short, I take and splice code to meet my needs, but this is beyond my understanding.
   I’d be very grateful for someone with knowledge to step me through this.
 * Humbly,
    Derek
 *  [Michael Fields](https://wordpress.org/support/users/mfields/)
 * (@mfields)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/simple-codexphp-coding-question-get_children/#post-2053028)
 * You can try something like this:
 *     ```
       <?php
       $images = get_children( array(
       	'post_parent'    => get_the_ID(),
       	'post_type'      => 'attachment',
       	'post_mime_type' => 'image',
       	'numberposts'    => -1
       	) );
       foreach ( (array) $images as $image ) {
       	print wp_get_attachment_image( $image->ID, 'thumbnail' );
       }
       ?>
       ```
   
 *  Thread Starter [derekbeck](https://wordpress.org/support/users/derekbeck/)
 * (@derekbeck)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/simple-codexphp-coding-question-get_children/#post-2053029)
 * Thanks Michael.
 * Just tried your code, but it gives results like this for each image:
 * `<img width="150" height="150" src="http://www.derekbeck.com/1775/wp-content/
   uploads/2011/04/Battle-of-Lexington-April-19-1775-The-Plate-I-Amos-Doolittle-
   17751-150x150.jpg" class="attachment-thumbnail" alt="Battle of Lexington, April
   19, 1775, The (Plate I) (Amos Doolittle, c. 1775)" title="Battle of Lexington,
   April 19, 1775, The (Plate I) (Amos Doolittle, c. 1775)" />`
 * The code is indeed finding just those attachments on that post, but I need merely
   the url link (to feed into facebook meta information). Any thoughts on how to
   do that?
 *  [Michael Fields](https://wordpress.org/support/users/mfields/)
 * (@mfields)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/simple-codexphp-coding-question-get_children/#post-2053030)
 * Please see: [http://codex.wordpress.org/Function_Reference/wp_get_attachment_url](http://codex.wordpress.org/Function_Reference/wp_get_attachment_url)
 *  Thread Starter [derekbeck](https://wordpress.org/support/users/derekbeck/)
 * (@derekbeck)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/simple-codexphp-coding-question-get_children/#post-2053031)
 * I tried putting the code
 * wp_get_attachment_url( $post_id )
 * in my header, but it renders nothing.
 * It doesn’t seem to fit in the code you offered above either.
 *  [Michael Fields](https://wordpress.org/support/users/mfields/)
 * (@mfields)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/simple-codexphp-coding-question-get_children/#post-2053032)
 * Try like this:
 *     ```
       <?php
       $images = get_children( array(
       	'post_parent'    => get_the_ID(),
       	'post_type'      => 'attachment',
       	'post_mime_type' => 'image',
       	'numberposts'    => -1
       	) );
       foreach ( (array) $images as $image ) {
       	print wp_get_attachment_url( $image->ID );
       }
       ?>
       ```
   
 *  Thread Starter [derekbeck](https://wordpress.org/support/users/derekbeck/)
 * (@derekbeck)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/simple-codexphp-coding-question-get_children/#post-2053033)
 * Now that works perfectly!
 * Thanks so very much, Michael! you are quite the expert!

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘simple codex/php coding question: get_children’ is closed to new replies.

## Tags

 * [images](https://wordpress.org/support/topic-tag/images/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 9 replies
 * 2 participants
 * Last reply from: [derekbeck](https://wordpress.org/support/users/derekbeck/)
 * Last activity: [15 years, 1 month ago](https://wordpress.org/support/topic/simple-codexphp-coding-question-get_children/#post-2053033)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
