questions

How to pull latest posts from a specific category

Rate this post

I’m trying to pull the latest posts from the category “local-news” but I’m only seeing one of the two posts that exist for this category. I’m not sure if it’s my if statement and/or while loop that’s causing the issue. This is my first time developing a WP theme and using PHP so I’m a little confused as to how I could fix this. I’m not sure how to close my if statement or while loop either without causing a critical error or syntax error.

 

<div class="container">
  <div class="row mt-4">
  <?php
    $args = array(
    'category_name' => 'local-news',
    'post_type' => 'post' ,
    'orderby' => 'date' ,
    'order' => 'DESC' ,
    'posts_per_page' => 6
    );
    $q = new WP_Query($args);
    $q -> the_post();
    ?>
    if ( $q->have_posts() ) : { 
      while ( $q->have_posts() ) : the_post() {
        <div class="card-body pb-0">
          <div class="latest_news_cont">
            <a href="<?php the_permalink() ?>">
              <?php the_post_thumbnail(); ?>
            </a>

            <a href="<?php the_permalink() ?>">
              <h5>
                <?php the_title(); ?>
              </h5>
            </a>
            <?php the_excerpt(); ?>
            <p style="text-align:center;"><a href="<?php the_permalink() ?>" class="readmore_news">Read more</a></p>
            <br>

          </div>
        </div>
        
        <div class="card-body pb-0">
          <div class="latest_news_cont">
            <a href="<?php the_permalink() ?>">
              <?php the_post_thumbnail(); ?>
            </a>

            <a href="<?php the_permalink() ?>">
              <h5>
                <?php the_title(); ?>
              </h5>
            </a>
            <?php the_excerpt(); ?>
            <p style="text-align:center;"><a href="<?php the_permalink() ?>" class="readmore_news">Read more</a></p>
            <br>
      </div>
    </div>
    
  </div>
</div>

 

✔️Solution:

I figured it out, had a lot of syntax issues and open php tags.

 

 <?php
  // the query
  $the_query = new WP_Query(array(
      'category_name' => 'local-news',
      'post_status' => 'publish',
      'posts_per_page' => 5,
  ));
  ?>

  <?php if ($the_query->have_posts()) : ?>
      <?php while ($the_query->have_posts()) : $the_query  >the_post(); ?>

    <div class="card-body pb-0">
    <div class="latest_news_cont">
        <a href="<?php the_permalink() ?>">
        <?php the_post_thumbnail(); ?>
        </a>

        <a href="<?php the_permalink() ?>">
        <h5>
            <?php the_title(); ?>
        </h5>
        </a>
        <?php the_excerpt(); ?>
        <p style="text-align:center;"><a href="<?php the_permalink() ?>" class="readmore_news">Read more</a></p>
        <br>

    </div>
    </div>

      <?php endwhile; ?>
      <?php wp_reset_postdata(); ?>

  <?php else : ?>
      <p><?php __('No News'); ?></p>
  <?php endif; ?>

Leave a Reply

Your email address will not be published.

Back to top button