• drinkingsouls

    (@drinkingsouls)


    Hey guys,
    I’m struggling to display thumbnails for related posts. I’m using a custom code to display related posts (by category) with their thumbnails. It currently works by grabbing the first image in the post, creating a thumbnail and displaying that. This works great and I want to keep this part, but I need it to be modified so that if the post contains no images it grabs the featured image for that post instead and displays that. I know this is kinda complex, is it possible?
    This is the code I currently use to grab related posts and display their thumbnails.

    <?php $orig_post = $post;
    						global $post;
    						$categories = get_the_category($post->ID);
    						if ($categories) {
    						$category_ids = array();
    						foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
    
    						$args=array(
    						'category__in' => $category_ids,
    						'post__not_in' => array($post->ID),
    						'posts_per_page'=> 2, // Number of related posts that will be shown.
    						'caller_get_posts'=>1
    						);
    
    						$my_query = new wp_query( $args );
    						if( $my_query->have_posts() ) {
    						echo '<div id="related-posts"><h2>Related News</h2><ul>';
    						while( $my_query->have_posts() ) {
    						$my_query->the_post();?>
    
    						<li><div class="relatedthumb"><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>">
    
    						<?php
    
    						$args = array(
    							'post_type'   => 'attachment',
    							'numberposts' => -1,
    							'post_status' => null,
    							'post_parent' => $post->ID,
    							'exclude'     => get_post_thumbnail_id()
    							);
    
    						$attachments = get_posts( $args );
    
    						if ( $attachments ) {
    							foreach ( $attachments as $attachment ) {
    								//echo apply_filters( 'the_title', $attachment->post_title );
    								the_attachment_link( $attachment->ID, false );
    							}
    						}
    
    						?>
    
    						</a></div>
    						<div class="relatedcontent">
    						<h3><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
    						Posted <?php the_time('F j, Y') ?>
    						</div>
    						</li>
    						<?
    						}
    						echo '</ul></div>';
    						}
    						}
    						$post = $orig_post;
    						wp_reset_query(); ?>

    Any help is greatly appreciated.
    Thank you.

  • The topic ‘Get featured image thubmanil’ is closed to new replies.