I use this code on my home page to display the latest posts:
It shows right the permalink to the post but no image.
My theme has the post thumbnail activated and works good on the posts page. I tried some methods, but I cannot pull the thumbnails. What do I need to add in the code from above?
I really appreciate any help.
Here is the proper code:
<ul>
<?php
global $post;
$tmp_post = $post;
$myposts = get_posts('numberposts=5&offset=1&category=1');
foreach($myposts as $post) :
setup_postdata($post);
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
<?php $post = $tmp_post; ?>
</ul>
<ul>
<li class="latest_thumb"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a></li>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<ul>
<?php endforeach; ?>
Resolved. With default thumbnails, I forgot to attach thumbs for the posts in that category. The following code will generate the latest 5 posts from category with id=7. Will show up linked thumbnails and titles to each post:
<?php
global $post;
$myposts = get_posts('numberposts=5&category=7');
foreach($myposts as $post) :
setup_postdata($post);
?>
<ul class="latest clearfix">
<li class="latest_thumb">
<?php
if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a>
<?php } else { ?>
<a href="<?php the_permalink() ?>"><img src="<?php bloginfo('template_directory') ;?>/i/news-img.jpg" alt=" "/></a>
<?php }
?>
</li>
<li class="latest_text">
<span class="date"><?php the_time('F j, Y'); ?></span>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
</ul>
<?php endforeach; ?>