admin管理员组

文章数量:1317564

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.

<?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>

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.

<?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>
Share Improve this question asked Nov 1, 2020 at 20:22 SalemSalem 1
Add a comment  | 

1 Answer 1

Reset to default 0

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; ?>

本文标签: theme developmentHow to pull latest posts from a specific category