• I have a homepage with a list of posts. The post consist of the exerpt, the title, and the first image from the post as thumbnail. The first 2 are no problem, the thumbnail is.

    The homepage consist of 3 loops. Two category loops and the standard loop.
    I found a function on this forums to get the image when you’re inside the loop.

    (forgot the link, so sorry for not giving credit)

    function display_thumbnail() {
    $files = get_children('post_parent='.get_the_ID().'&post_type=attachment&post_mime_type=image');
    
      if($files) :
        $keys = array_reverse(array_keys($files));
        $j=0;
        $num = $keys[$j];
        $image=wp_get_attachment_image($num, 'large', false);
        $imagepieces = explode('"', $image);
        $imagepath = $imagepieces[1];
        $thumb=wp_get_attachment_thumb_url($num);
        print "<img src='$thumb' class='thumbnail' />";
    
      endif;
    }

    This is the code for my self-made loops

    <?php $my_query = new WP_Query('category_name=sticky_2&showposts=1'); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    // HTML Template code of my post including call to function display_thumbbnail()
    <?php endwhile ?>

    Only the images in de default wordpress loop are working. The functions doesn’t seems to work in my loops.

    I found that $files is NULL when the function is called from inside my loop. The function get_post_ID() return the right ID. So I conclude that get_children is not working when called from inside my loop.

    Anyone has a clue how to get this working?

    thx alot!!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi,

    Check with this code:

    <?php query_posts("cat=3&showposts=5"); ?>
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    $files = get_children(
    array(
    	'post_parent' => $post->ID,
    	'post_status' => 'inherit',
    	'post_type' => 'attachment',
    	'post_mime_type' => 'image',
    	'order' => 'ASC',
    	'orderby' => 'menu_order'
    	)
    );
    if($files) {
    	$keys = array_keys($files);
        $num = $keys[0];
    	$firstImageSrc = wp_get_attachment_image_src($num, "medium", $icon);
    	if ($firstImageSrc <> null) {
    	echo "<img src=\"{$firstImageSrc[0]}\" width=\"120\" alt=\"$title\" />"; }
    <?php endwhile; ?>
    <?php else : ?>
    <h2>Not Found</h2>
    <?php endif; ?>

    Thanks,

    Shane G.

    Thread Starter tstruyf

    (@tstruyf)

    1. No output, $files is still empty.

    2. Even if it worked , I don’t think it could solve my situation. I use custom loops with instances of the WP_Query class because I don’t want to mess up default query. This is because I use pages and if I use the query_post() more then once on a page, the paging is broken.

    edit: forgot to say that the posts show fine, just no images with the code above.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘get_children() inside custom loop’ is closed to new replies.