カスタム分類タイプごとにカスタム投稿タイプを繰り返しますか? (カテゴリごとにWordPressの投稿を注文するか、分類学期ごとにカスタム投稿タイプを表示する)

StackOverflow https://stackoverflow.com/questions/3848608

  •  27-09-2019
  •  | 
  •  

質問

カテゴリで区切られたすべての投稿を表示するページが必要です。アイデアは、カテゴリを取得し、各カテゴリのすべての投稿を繰り返すことです。この問題は、カテゴリとしてカスタム分類法を使用して、特定のカスタムタイプのすべての投稿を反復したいという事実によって複雑になります。 (WordPressを実行する3)

私のfunctions.phpでは、カスタム投稿タイプは「ビデオ」として登録され、カスタム分類法は「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; ?>
役に立ちましたか?

解決

Jeezは、カスタム分類法の各項目が用語と呼ばれることを理解すると(NoobのWordPressドキュメントではすぐには明らかではありません)、検索がはるかに簡単です。このソリューションは、すべてのカスタムクエリのものがなければ理解しやすいです。

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

そして、WordPress Codexで上記の関数を検索することにより、理解のギャップを入力できます。上記のコードでは、私の特定のアプリケーションの場合、custom_post_type_nameはビデオであり、taxonomy_nameはvideo_type(またはvideo_types、忘れます)になります。

他のヒント

別のアプローチを試すことができます。 get_postsを使用して投稿をカスタム分類法でソートし、最初は空の文字列($ current_catなどと呼ばれる)である変数を設定し、結果の各ループで、分類法を確認し、$ current_catと比較してください。違うには、新しいカテゴリのヘッダーを印刷してから、エントリが同じ場合は、通常どおりエントリを印刷します。

あなたのコードに関する明確な問題(私は信じている)は、あなたがあなたのカスタム分類法を適切に照会していないことです。単純に使用する必要があります 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を使用するのに役立つ場合があります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top