• endless-loving

    (@endless-loving)


    Hi there,

    I am trying to create a custom front page for a blog where I display a post thumbnail (featured image) or an attachment image, if the featured image is not set.

    I get the featured image to display but there is no attachment image showing up with this code, even though there is an image in the post content.

    // options come from custom field
    	$args = array(
    		'cat'=>$blog_exclude,
    		'posts_per_page'=>$posts_per_page,
    		'paged'=>$paged
    	);
    	query_posts($args);
    	// $posts_remaining = $wp_query->found_posts-$offset;
    	while (have_posts()) : the_post(); 
    
    	// the current post has a thumbnail (featured image)
    	if (has_post_thumbnail()) {
    		$attr = array(
    			'class'	=> "post-thumbnail",
    		);
    		echo '<a href="' , the_permalink() , '" class="post-thumb-link">';
    		the_post_thumbnail('full', $attr);
    		echo '</a>';
    		$has_thumb = ' with-img';
    
    	// the current post has NO thumbnail
    	} else {
    
    		// check for attachments
    		$args = array(
    			'post_type' => 'attachment',
    			'numberposts' => -1,
    			'post_parent' => $post->ID,
    		);
    		$attachments = get_posts( $args );
    		// display the first image if there is one
    		if ( $attachments ) {
    			foreach ( $attachments as $attachment ) {
    				echo '<a href="' , the_permalink() , '" class="post-thumb-link">';
    				echo wp_get_attachment_image( $attachment->ID, 'medium' );
    				echo '</a>';
    			  }
    
    			$has_thumb = ' with-img';
    		 }
    		}

    Any ideas?

  • The topic ‘Trying to get post thumbnail or attachment’ is closed to new replies.