Hi all, I'm using this piece of code I found (http://impnerd.com/wordpress-hack-add-post-images-to-your-homepage) to display the first image uploaded to a post on the homepage next to the excerpts of posts. I'm doing this outside the main loop on the homepage, and have been having problems. When I do rewind_posts() to get the same loop results, it works fine, but when I try to create a different loop, this code snippet breaks down:
$images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID );
if ($images)
{
$keys = array_keys($images);
$num = $keys[0];
$firstImageSrc = wp_get_attachment_thumb_url($num);
echo "<li><img src=\"{$firstImageSrc}\" width=\"288\" height=\"216\" alt=\"\" title=\"\" /></li>";
}
I have tried the methods in The_Loop#Multiple_Loops_in_Action in the docs, and they work, meaning I can get normal output after the loop, but my snippet above doesn't work. Any idea if there is a conflicting method call or something that's going on that's stopping it from working? Would appreciate some help, thanks!
To be more specific:
<?php $my_query = new WP_Query('category_name=Daily Photo&showposts=1');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID; ?>
<p>a</p>
<?php $images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID );
if ($images) {
$keys = array_keys($images);
$num = $keys[0];
$firstImageSrc = wp_get_attachment_thumb_url($num);
echo "<li><img src=\"{$firstImageSrc}\" width=\"288\" height=\"216\" alt=\"\" title=\"\" /></li>";} ?>
<?php endwhile; ?>
will output <p>a</p>
, but not the <li><img /></li>
code I need in the snippet. Whereas if I use rewind_posts();, everything works, and I get the <li><img /></li>
code, but I don't want to use the same loop that I had been using previously. I'm using this to display a daily photo in the sidebar, that pulls from the "Daily Photo" category. I will exclude Daily Photos from the main loop, and only want to use them to draw images from in that snippet.