WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] simple codex/php coding question: get_children (10 posts)

  1. derekbeck
    Member
    Posted 2 years ago #

    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?

  2. derekbeck
    Member
    Posted 2 years ago #

    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 ) . " -->";
    
    	}
    }
  3. Michael Fields
    Themer
    Posted 2 years ago #

  4. derekbeck
    Member
    Posted 2 years ago #

    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

  5. Michael Fields
    Themer
    Posted 2 years ago #

    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' );
    }
    ?>
  6. derekbeck
    Member
    Posted 2 years ago #

    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?

  7. Michael Fields
    Themer
    Posted 2 years ago #

  8. derekbeck
    Member
    Posted 2 years ago #

    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.

  9. Michael Fields
    Themer
    Posted 2 years ago #

    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 );
    }
    ?>
  10. derekbeck
    Member
    Posted 2 years ago #

    Now that works perfectly!

    Thanks so very much, Michael! you are quite the expert!

Topic Closed

This topic has been closed to new replies.

About this Topic