Displaying last 3 blog posts on wordpress custom theme

Solution:1

Try the snippet below. Use your custom HTML block (the one with botom_box class) instead of this used below.

<?php $posts = get_posts("numberposts=3"); ?>
<?php if($posts) : ?>
    <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
        <!-- your HTML block goes here --->
        <div class="post">
        <h3><a href="<?php echo get_permalink($post->ID); ?>" ><?php echo $post->post_title; ?></a></h3>
        <?php the_excerpt(); ?>" rel="bookmark">read more</a>
        </div>
        <!-- end of the HTML block -->
    <?php endforeach; ?>
<?php endif; ?>

Solution:2

More advanced WordPress users may want to add recent posts directly in their WordPress theme files. Of course, you should use a child theme for this so that when you update your theme, you don’t lose your changes.

It’s always a good idea to create a backup before you edit your theme files. If anything does go wrong, you might want to take a look at our list of the most common WordPress errors and how to fix them.

The easiest way to manually display recent posts to use the built-in WP_Query class. Simply add this code where you want to display the recent posts.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<ul>
 
<?php
// Define our WP Query Parameters
$the_query = new WP_Query( 'posts_per_page=5' ); ?>
 
<?php
// Start our WP Query
while ($the_query -> have_posts()) : $the_query -> the_post();
// Display the Post Title with Hyperlink
?>
 
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
 
<li><?php
// Display the Post Excerpt
the_excerpt(__('(more…)')); ?></li>
 
<?php
// Repeat the process and reset once it hits the limit
endwhile;
wp_reset_postdata();
?>
</ul>

This code displays the five most recent posts with their title and excerpt. The WP_Query class has tons of parameters that allow you to customize it any way that you like. For more information please refer to the WordPress developer documentation.