0

I am trying to show the three most recent posts in the category "everything". I don't want these posts to show in a repeatable HTML markup - instead the first one displays in a row, aligned left. The next post displays a row below, aligned right. The final, a third row, aligned left again. I am able to collect the posts but am finding it hard to know where call them within my HTML.

Right now, my code repeats the whole page content three times, instead of three different posts once.

<?php $args = array(
'post_type' => 'project',
'post_status' => 'publish',
'category_name' => 'everything',
'posts_per_page' => 3, );
$the_query = new WP_Query( $args );
?>

<?php if (have_posts()) : while ( $the_query->have_posts()) : $the_query->the_post(); ?>
<section class="container clearfix">
<div class="row features">
  <a href="<?php the_permalink() ?>">
    <figure class="grid-10-lg">


      <?php 
        $image = get_field('front_page_image');
        if( !empty($image) ): ?>
        <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
      <?php endif; ?>
      <figcaption>
        <h3>Latest Post:
          <span><?php the_title(); ?></span>
        </h3>
      </figcaption>
    </figure>
  </a>
0

Possible you've forgotten to reset post with wp_reset_postdata function.

You can use get_posts instead of new WP_Query:

<?php foreach(get_posts($args) as $post) : ?>
    <div class="post">
        <a class="post-link" href="<?= get_permalink($post->ID) ?>" target="_blank"><?= $post->post_title; ?></a>
    </div>
<?php endforeach; ?>

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.