I want to display the latest 2 blog posts in my footer. I used while (have_posts()) : the_post();
to display all the blog posts in my blog page ( Almost it's the index page in my case ).
When I tried to filter the latest to blog posts using the same approach I was unable to achieve that.
This is the code so far I have tried. I used a for loop
to limit the number of posts.
<ul class="widget-post-list">
<?php if (have_posts()) : while (have_posts()) : the_post(); for ($i = 0; $i <2; $i ++){ ?>
<li>
<h5 class="post-title"><a href="<?php echo get_permalink() ?>"><?php echo wp_trim_words(get_the_title(), 14); ?></a></h5>
</li>
<?php } endwhile; else: ?>
<h5>No Posts found.</h5>
<?php endif; ?>
</ul>
Through this code I am only returned the
Home
page link twice.
What is problem here? or is there any other way that I can try?