admin管理员组

文章数量:1401176

I have a problem.

<div class="list">
  <?php
    $categories = get_the_category();
    $category_id = $categories[0]->cat_ID;
    $args = array(
      'posts_per_page' => -1,
      'offset' => 0,
      'orderby' => 'date',
      'order' => 'ASC',
      'cat' => $category_id
    );
    $products = get_posts( $args );
  ?>
  <?php foreach ( $products as $post ) : setup_postdata( $post ); ?>
    <div class="list__item">
      <a href="<?php the_permalink(); ?>">
        <figure>
          <img src="<?php the_post_thumbnail_url('thumbnails--products'); ?>" alt="">
        </figure>
        <h3><?php the_title(); ?></h3>
      </a>
    </div>
  <?php endforeach; wp_reset_postdata(); ?>
</div>

If I understand correctly, now I should display posts from the category I am currently in because I am retrieving her ID, but posts from the main category Posts are displayed.

I have a problem.

<div class="list">
  <?php
    $categories = get_the_category();
    $category_id = $categories[0]->cat_ID;
    $args = array(
      'posts_per_page' => -1,
      'offset' => 0,
      'orderby' => 'date',
      'order' => 'ASC',
      'cat' => $category_id
    );
    $products = get_posts( $args );
  ?>
  <?php foreach ( $products as $post ) : setup_postdata( $post ); ?>
    <div class="list__item">
      <a href="<?php the_permalink(); ?>">
        <figure>
          <img src="<?php the_post_thumbnail_url('thumbnails--products'); ?>" alt="">
        </figure>
        <h3><?php the_title(); ?></h3>
      </a>
    </div>
  <?php endforeach; wp_reset_postdata(); ?>
</div>

If I understand correctly, now I should display posts from the category I am currently in because I am retrieving her ID, but posts from the main category Posts are displayed.

Share Improve this question asked Feb 7, 2020 at 12:14 Piotr WawrzyniakPiotr Wawrzyniak 132 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

get_the_category returns posts only with the default taxonomy "category", if you are trying to display posts from a custom registered taxonomy you should use get_the_terms

Also get_posts argument category doesn't support custom taxonomies, you should use WP_Query's Taxonomy Query mostly.

本文标签: Problem with displaying posts in the CPT category