vikkineal
Member
Posted 3 months ago #
Hi
I want to display a panel of 3 recent posts, their image and an excerpt of the post on my homepage. The code I have is out of the loop and while it works to some extent (does pull an excerpt for one of the posts) - it actually repeats the same excerpt for all 3.
<ul>
<?php
$IDOutsideLoop = $post->ID;
global $post;
$myposts = get_posts('showposts=3');
foreach($myposts as $post) :
?>
<li>
<?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
<?php echo substr(get_the_excerpt(), 0,30); ?>
</li>
<?php endforeach; ?>
</ul>
Any ideas what I'm doing wrong?
Thanks :)
Danny de Haan
Member
Posted 3 months ago #
Instead of using get_the_excerpt(), you can try to use $post->post_excerpt. Also for your thumbnail, use $post->ID.
vikkineal
Member
Posted 3 months ago #
Hi - thanks for the advice. I'm still getting my head around everything so just want to check that you mean like I've written below?
<li>
<?php echo get_the_post_thumbnail($post->ID, 'thumbnail'); ?>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
<?php echo substr($post->post_excerpt); ?>
</li>
I'm assuming that will default to the 55 character excerpt? To increase to 200 do i just write...?
<?php echo substr($post->post_excerpt, 0,200); ?>
vikkineal
Member
Posted 3 months ago #
Hi Esmi, is your suggestion instead of this snippet in my code?
$myposts = get_posts('showposts=3');
foreach($myposts as $post) :
vikkineal
Member
Posted 3 months ago #
Thank you for all your help, here is the final code;
<div id="latestNewsPanel">
<h3><a href="#">Latest news</a></h3>
<ul>
<?php
$args = array( 'numberposts' => 3 );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post); ?>
<li><?php the_title(); ?>
<a href="<?php the_permalink(); ?>"><?php echo get_the_post_thumbnail($post->ID, 'thumbnail'); ?></a>
<p><?php echo substr($post->post_excerpt, 0,200); ?><a href="<?php the_permalink(); ?>">Read more</a></p>
</li>
<?php endforeach; ?>
</ul>
<div class="clearBoth"></div>
</div>