prosource

사용자 지정 분류법 유형별로 사용자 지정 게시물 유형을 반복하시겠습니까? (범주별로 워드프레스 게시물 정렬 또는 분류법 용어별로 사용자 지정 게시물 유형 표시)

probook 2023. 3. 4. 15:00
반응형

사용자 지정 분류법 유형별로 사용자 지정 게시물 유형을 반복하시겠습니까? (범주별로 워드프레스 게시물 정렬 또는 분류법 용어별로 사용자 지정 게시물 유형 표시)

모든 투고를 카테고리별로 구분하여 보여주는 페이지를 원합니다.아이디어는 카테고리를 취득한 후 각 카테고리의 모든 투고를 반복하는 것입니다.이 문제는 커스텀 분류법을 카테고리로 사용하여 특정 커스텀타입의 모든 투고를 반복하는 것으로 인해 복잡합니다.(Running Wordpress 3)

functions.php에서는 커스텀 투고 타입이 "video"로 등록되고 커스텀 분류법이 "video_types"로 등록됩니다.

카테고리별로 정렬된 모든 비디오를 표시하도록 되어 있는 커스텀 페이지 템플릿에서는, 다음의 코드가 투고를 반환하지 않습니다(그리고, 거기에 있는 것을 확인했습니다.

<?php 
  $categories = get_categories(array(
    'taxonomy' => 'video_types'
  )); 
  foreach ($categories as $cat):
?>
 <section id="<?php $cat->slug ?>" class="video-category">
     <?php
  query_posts(array(
      'cat' => $cat->cat_ID,
      'posts_per_page' => -1
         ));
     ?>
     <h2><?php single_cat_title(); ?></h2>
    <p class="description"><?php echo category_description($cat->cat_ID); ?></p>
  <?php while (have_posts()) : the_post(); ?>
      <?php
       $category = get_the_category(); 
            echo $category[0]->cat_name;
      ?>
      <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
      <article class="video">
        <h3><?php the_title(); ?></h3>
        <p>
          <?php the_content() ?>
        </p>
      </article>
  <?php endwhile; ?>
 </section>
<?php endforeach; ?>

이런, 커스텀 분류법의 각 항목이 용어라고 불리는 것을 알게 되면(noob의 워드프레스 문서에서는 금방 알 수 없음), 검색하기가 훨씬 쉬워집니다.이 솔루션은 커스텀 쿼리 기능이 없어도 이해하기 쉬워집니다.

<?php
// A term is an item of a taxonomy (e.g. "Promotional" could be a term for the taxonomy "video_type")
// ...so $categories could be $terms and it would still make sense
$categories = get_terms('taxonomy_name');
foreach( $categories as $category ):
?>
  <section class="category-<?php echo $category ?>">
    <h2><?php echo $category->name; // Print the cat title ?></h2>
    <p class="description"><?php echo $category->description ?></p>
    <div class="<?php echo $category->post_type ?>-list">
      <?php
      //select posts in this category (term), and of a specified content type (post type) 
      $posts = get_posts(array(
        'post_type' => 'custom_post_type_name',
        'taxonomy' => $category->taxonomy,
        'term' => $category->slug,
        'nopaging' => true, // to show all posts in this category, could also use 'numberposts' => -1 instead
      ));
      foreach($posts as $post): // begin cycle through posts of this category
        setup_postdata($post); //set up post data for use in the loop (enables the_title(), etc without specifying a post ID)
      ?>
        // Now you can do things with the post and display it, like so
        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
          <h3><?php the_title(); ?></h3>
          <?php 
            // Getting custom field data example
            echo get_post_meta($post->ID, 'field_key', true); 
          ?>
          <?php the_content() ?>
        </article>
      <?php endforeach; ?>
    </div>
  </section>
<?php endforeach; ?>

그리고 워드프레스 코덱스에서 위의 함수를 검색함으로써 이해의 공백을 메울 수 있다.위의 코드에서 특정 어플리케이션의 경우 custom_post_type_name은 video, taxonomy_name은 video_type(또는 video_types, 잊어버렸습니다)이 됩니다.

다른 방법을 시도해 보세요.get_posts를 사용하여 투고를 커스텀 분류법에 따라 정렬하고 처음에 빈 문자열($current_cat 등)인 변수를 설정하고 결과의 각 루프를 체크하여 $current_cat과 비교합니다.다른 경우 새 카테고리의 헤더를 인쇄하고 엔트리가 같은 경우 en을 인쇄합니다.여느 때처럼 해보다

코드의 분명한 문제점은 커스텀 분류법을 제대로 조회하지 않는다는 것입니다.단순하게 사용해야 합니다.taxonomy_name => 'value'쿼리 내에서 커스텀 분류법에는 영향을 받지 않습니다.cat질문에서.

더 자세한 정보가 필요하시면 말씀하세요.

편집: 상세!

// get a list of categories, in this case your custom taxonomy (your_taxonomy_name)
$querystr = "SELECT terms.* FROM $wpdb->term_taxonomy tax LEFT JOIN $wpdb->terms terms ON tax.term_id = terms.term_id WHERE tax.taxonomy = 'your_taxonomy_name'";

$categories = $wpdb->get_results($querystr, OBJECT);

foreach( $categories as $category ): // begin a loop through those terms (categories in your custom taxonomy)
    echo '<div class="category-header"><h3>'.$category->name.'</h3>'; // print the cat title
    echo '<p class="category-description">'.strip_tags(term_description($category->term_id,'your_taxonomy_name')).'</p></div>'; // cat description

    $posts = get_posts( array( 'your_taxonomy_name' => $category->name, 'post_type' => 'your_post_type' ) ) //select posts in this category, and of a specified content type
    foreach($posts as $post) : // begin cycle through posts of this category
        setup_postdata($post); //set up post data for use in the loop (enables the_title(), etc without specifying a post ID)

        [ ... ] // do things with your post (display it)

    endforeach;

endforeach;

이것으로 충분합니다., get_posts 의 사용에 도움이 될 가능성이 있습니다.

언급URL : https://stackoverflow.com/questions/3848608/iterate-through-custom-post-type-by-custom-taxonomy-type-ordering-wordpress-po

반응형