0

I'm new to WordPress and trying to turn a website into a WordPress theme. At the bottom of the index page (static page) there is an area for the two latest posts. I have created two dummy posts on the backend to test it. When I try to display the posts on the index page, it displays the title and content of the home page rather than the content of the most recent posts.

Here's the code I wrote;

<?php $i = 0; while (have_posts() && $i < 3) : the_post(); ?>
    <div class="blog-cards">
        <img src="<?php bloginfo('template_url'); ?>/img/highlight.jpg" alt="Blog Image">
        <span class="meta"><small><?php the_date(); ?></small></span>
        <h3 class="blog-title"><?php the_title(); ?></h3>
        <p class="blog-body"><?php the_excerpt(); ?></p>
   </div>
<?php $i++; endwhile; ?>   
  • Are you on a page template? Whenever using "the loop" on a page template, it will assume you want the title/info from that page. If you want posts while in a page template, then you're heading in the right direction by using queries. Here's an example on the Codex with WP_Query: developer.wordpress.org/reference/classes/wp_query/… Good luck! – RachieVee Aug 2 '17 at 20:23
0

I figured out a solution. It works, not entirely sure if it's best practice but I queried the posts first and it seems to show up.

Here's my code;

<?php query_posts('showposts=2'); if (have_posts()) : while (have_posts()) : the_post(); ?>

I then closed the if statement with;

<?php endif; wp_reset_query(); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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