0

I'm trying to get the list of images in my post, to display them in the front page... the problem is that when I add a new picture from a URL, it doesn't seem to attach it.

$images =& get_children( 'post_type=attachment&post_mime_type=image' );

This returns an empty array... how can I solve this so I can actually use these images and create thumbnails to display them on the front?

1

A quick experiment with WordPress 3.5.1 shows that when you an image into post from URL, there's no post inserted into the wp_posts or wp_postmeta table - which is why the $images array is coming back empty.

You could write some JS on the admin side to create and insert a post asynchronously when you click the insert into post button - otherwise, you could use some Regex with get_the_content() to pull them from the content, which is likely more complicated and less reliable.

EDIT Below is only applicable when uploading media and attaching to posts.

Straight from the WordPress Codex wp_get_attachment_image page:

To display all of the images and titles attached to a certain page and display them as a list of bullets you can use the following:

<ul>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();    

 $args = array(
   'post_type' => 'attachment',
   'numberposts' => -1,
   'post_status' => null,
   'post_parent' => $post->ID
  );

  $attachments = get_posts( $args );
     if ( $attachments ) {
        foreach ( $attachments as $attachment ) {
           echo '<li>';
           echo wp_get_attachment_image( $attachment->ID, 'full' );
           echo '<p>';
           echo apply_filters( 'the_title', $attachment->post_title );
           echo '</p></li>';
          }
     }

 endwhile; endif; ?>
</ul>
  • you didn't read the post. The problem is that images that are added "via url" are not attached to the post – user5057 May 5 '13 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.