programing

WP_Wordpress의 카테고리별 게시물 표시 쿼리(커스텀 게시물 유형)

sourcejob 2023. 3. 25. 11:04
반응형

WP_Wordpress의 카테고리별 게시물 표시 쿼리(커스텀 게시물 유형)

헤즈, 짧게 할게요.WP 루프로 출력합니다.

Support
    Category1
      -Post1
      -Post2
    Category2
      -PostA
      -PostB
      -PostC

그래서 커스텀 포스트 타입에 있는 카테고리별로 포스트를 주문하고 싶습니다.support([Types]플러그인 링크(ujeb.se/A4zqZ)에 의해 작성되었습니다.

이거 있어요.

<?php
$args = array('post_type' => 'support');
$query = new WP_Query($args);

while($query -> have_posts()) : $query -> the_post(); ?>

    <p><?php the_category(); ?></p>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <p><?php the_content(); ?></p>

<?php endwhile; ?>

나의$query커스텀 투고 타입에서 필요한 투고를 모두 저장합니다(support카테고리별로 표시하는 데 문제가 있습니다.뭔가 필요한 게 있을 것 같아foreach잘 모르겠어요.좋은 의견이라도 있나?

/편집/
현재 디스플레이는 다음과 같습니다.

Support, Category1
Post1
---
Support, Category2
PostA
---
Support, Category1
Post2

etc.

방법은 다음과 같습니다.카테고리를 순환하기 위해서는 포어치 루프가 필요했습니다.

<?php
$cats = get_categories();

foreach ($cats as $cat) {
$args = array(
'post_type' => 'support',
'tax_query' => array(
    array(
        'taxonomy' => 'category',
        'field'    => 'term_id',
        'terms'    => $cat->cat_ID,
        ),
    ),
);
$query = new WP_Query($args);

if ( $query->have_posts() ): ?>
    <p><?php echo $cat->cat_name ; ?></p> <?

   while($query -> have_posts()) : $query -> the_post(); ?>
      <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
      <p><?php the_content(); ?></p> <?php
   endwhile;
endif; 

// Added this now 
wp_reset_query() ; 
}

이 작업은 다음과 같습니다.

   $posts = new WP_Query(array(
        'category_name' => 'news,
        'post_status' => 'publish',
        'post_type' => 'post',
        'posts_per_page' => 6,
        ));

언급URL : https://stackoverflow.com/questions/31973457/wp-query-to-display-posts-by-category-in-wordpress-in-custom-post-types

반응형