• Was looking for a plugin (that worked with v3) or a tag that I can get all images from within a post and list them separate from content. Best i have seen is get first image only.

    ie/ for each
    `<a><img></a>
    <a><img></a>
    <a><img></a>`

    So far something with strip_tags looks like my only option. Anyone ever do something like this?

Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter Centinal

    (@centinal)

    Thanks for the response. would this work in the loop tho?

    Like this

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>    
    
    <?php the_title(); ?>
    
    <!--images from post here-->
    <?php the_content(); ?> <!--without images-->
    
     <?php endwhile; endif; ?>

    Well, the working part of that code would be whatever you used in place of <!--images from post here-->.

    Thread Starter Centinal

    (@centinal)

    not having luck >< this is the code i have put on a page template. all i want is the images that are posted in the post body. tried using the code from get_children

    <?php
    // query post 154
    query_posts('p=154');
    // the Loop
    while (have_posts()) : the_post();
    // the content of the post
    $images =& get_children( 'post_type=attachment&post_mime_type=image' );
    if ( empty($images) ) {
    // no attachments here
    } else {
    foreach ( $images as $attachment_id => $attachment ) {
    echo wp_get_attachment_image( $attachment_id, 'full' );
    }
    }
    endwhile;
    ?>

    Try replacing:

    $images =& get_children( 'post_type=attachment&post_mime_type=image' );
    if ( empty($images) ) {
    	// no attachments here
    } else {
    	foreach ( $images as $attachment_id => $attachment ) {
    		echo wp_get_attachment_image( $attachment_id, 'full' );
    	}
    }

    with:

    $args = array(
    	'post_type' => 'attachment',
    	'numberposts' => -1,
    	'post_status' => null,
    	'post_parent' => $post->ID
    	);
    $images = get_posts($args);
    if ($images) {
    	foreach ($images as $image) {
    		echo wp_get_attachment_image( $attachment_id, 'full' );
    	}
    }
    Thread Starter Centinal

    (@centinal)

    Appreciate the help ~ not working for me though. Does a image in the_content count as an attachment? Maybe thats the problem.

    Does a image in the_content count as an attachment?

    If it was uploaded via Add Image whilst editing the relevant page or post, yes – it’s an attachment to that post or page.

    If it was uploaded directly to the Media Library, it won’t count as an attachment.

    Thread Starter Centinal

    (@centinal)

    Gah, uploaded with media library and attached to a page already. Is there no way to attach to multiple posts? If not I guess I could re-upload them. But they are already cropped/titled, was hoping could make this easy.. lol ><

    Thanks a lot esmi, great advice. Im going to mess around with this a bunch more. Now that I have a better understanding how attachments work.

    Is there no way to attach to multiple posts?

    Sorry – no. ??

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Get all images from within post’ is closed to new replies.