1

I'm trying to display the two recent posts in a bloc I created in a front page of my website.

index.html:

<div id="blog">
<h1>
Post title 1
</h1>
<p>
Description
</p>
<button>
Read More
</button>
<h1>
Post title 2
</h1>
<p>
Description
</p>
<button>
Read More
</button>
</div>

I want that the Post title 1 and Description of each post will be updated automatically when the admin add a new post and the recent post will be displayed on the front page. I installed the insert php to add php code in a html page, then I wrote this snippet of code to test if I'm able to get the posts from the blog:

[insert_php]
if ( have_posts() ) :
    while ( have_posts() ) : the_post();
        echo "test successful";
    endwhile;
else :
    echo wpautop( 'Sorry, no posts were found' );
endif;
[/insert_php]

But I got Sorry, no posts were found however I had some posts in the blog page of my website.How can I fix that to display the latest recent posts in my front page?

1
  • Is it WordPress related? Are using a plugin?
    – Max Yudin
    Commented May 24, 2017 at 10:16

1 Answer 1

0

Try to implement this program into your html page:

<ul>
<?php

global $post;
$args = array( 'posts_per_page' => 2);

$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
    <li>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        <?php the_content(); ?>
    </li>
<?php endforeach; 
wp_reset_postdata();?>

</ul>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.