Getting attachment images in the sidebar
-
I am trying to get the thumbnails from a categories posts into the sidebar (outside the loop) but can’t seem to work it. I am trying to use the following function:
<?php # Displays post image attachment (sizes: thumbnail, medium, full) function side_th_image($postid=0, $size='thumbnail', $attributes='') { if ($postid<1) $postid = get_the_ID(); if ($images = get_children(array( 'post_parent' => $postid, 'post_type' => 'attachment', 'numberposts' => 1, 'post_mime_type' => 'image',))) foreach($images as $image) { $attachment=wp_get_attachment_image_src($image->ID, $size); ?><?php echo $attachment[0]; ?><?php } } ?>
In the following sidebar code:
<h2>From the collection</h2> <ul> <?php global $post; $myposts = get_posts('category=1&numberposts=5&orderby=rand'); foreach($myposts as $post) : ?> <li><img src="<?php side_th_image(); ?>"></li> <?php endforeach; ?> </ul>
Any ideas where I’m going wrong? This works fine when I call the code into the loop.
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Getting attachment images in the sidebar’ is closed to new replies.