• I have a template that explodes all of my categories and pulls the latest post title, excerpt and attached thumbnail for the latest post in each category. I am using the ideas from this post in my functions.php file:

    https://www.ads-software.com/support/topic/140609?replies=40#post-779279

    This is what I have in functions.php:

    function postimage($size=medium) {
    	if ( $images = get_children(array(
    		'post_parent' => get_the_ID(),
    		'post_type' => 'attachment',
    		'numberposts' => 1,
    		'post_mime_type' => 'image',)))
    	{
    		foreach( $images as $image ) {
    			$attachmenturl=wp_get_attachment_url($image->ID);
    			$attachmentimage=wp_get_attachment_image( $image->ID, 'thumbnail' );
    			$imagelink=get_permalink($image->post_parent);
    
    			echo '<a href="'.$imagelink.'">'.$attachmentimage.apply_filters('the_title', $parent->post_title).'</a>';
    		}
    	} else {
    		echo "No Image";
    	}
    }

    Here is part of the code that explodes my category posts:

    global $post;
    							$myposts = get_posts("numberposts=1&category=$cat_number");
    							foreach($myposts as $post)
    								{
    									$posttitle = get_the_title();
    									$posttime = get_the_time('F jS, Y');
    									$postauthor = get_the_author();
    									$postlink = get_permalink();
    									$thumbnail = postimage('thumbnail');
    									$text = get_the_content();
    									$text = substr($text , 0, 140);
    									echo '<div class="article">';
    									echo '<a href="'.$postlink.'">'.$thumbnail.'</a>';
    									echo '<h4><a href="'.$postlink.'" title="'.$posttitle.'">'.$posttitle.'</a></h4>';
    									echo '<div class="byline">by '.$postauthor.' | '.$posttime.'</div>';
    									echo '<p>'.$text.'[...] <a href="'.$postlink.'" title="Read More">Read More &raquo;</a></p>';
    									echo '</div>';
    									echo '</div>';
    								}

    The problem is, the code is pulling the thumbnail from the first post with an attachment. I need it to pull the thumbnail from the post that it is attached to.

    WordPress gods? You there? Can you help?

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Help with wp_get_attachment_image’ is closed to new replies.