WordPress.org

Support

Support » Plugins and Hacks » Hacks » [Resolved] simple codex/php coding question: get_children

[Resolved] simple codex/php 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)
  • 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 ) . " -->";
    
    	}
    }

    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

    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' );
    }
    ?>

    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?

    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.

    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 );
    }
    ?>

    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 ‘[Resolved] simple codex/php coding question: get_children’ is closed to new replies.
Skip to toolbar