2012-05-08 3 views
1

長い質問タイトルは残念です。私は正確であるように試みていました。WordPress - カスタムポストの種類とカテゴリ別のポストを取得する(各カテゴリは自動的にDIVで囲まれる)

特定のカスタム投稿タイプから投稿を自動的に取得し、各投稿がどのように分類されているかを検出し、各カテゴリが独自のDIVで囲まれたページにカテゴリ別投稿を出力するWordPressクエリを考案する必要があります。

たとえば、「地図データ」というカスタムの投稿タイプがあります。このカスタムポストタイプでは、私は「カテゴリ」という名前の階層構造を持ち、その分類内には多数のカテゴリ「カテゴリ#1」「カテゴリ#2」などがあります。各カテゴリには多数の投稿があります。

ので、クエリは、カスタムポストタイプ内のすべてのカテゴリのリストを取得した後、出力する必要があり、このような何か:

<div id="category-1"> 
    <div class="post">This is a post in Category 1</div> 
    <div class="post">This is another post in Category 1</div> 
</div> 
<div id="category-2"> 
    <div class="post">This is a post in Category 1</div> 
    <div class="post">This is another post in Category 1</div> 
</div> 

私はデフォルトのWordPressのカテゴリーシステムで動作する次のコードを持っていますしかし、私はそれを書き直すか、それを更新してカスタムの投稿タイプとその分類法で動作させる必要があります。

<?php 
    $cat_args=array(); 
    $categories=get_categories($cat_args); 
    foreach($categories as $category) { 
     $args=array(
      'category__in' => array($category->term_id), 
     ); 
    $posts=get_posts($args); 
     if ($posts) { 
      echo '<div class="cat" id="' . $category->slug.'" name="' . $category->name.'">'; 
      foreach($posts as $post) { 
      setup_postdata($post); 
?> 

<?php the_title();?> 
<?php the_content();?> 

<?php 
     } // foreach($posts 
     echo '</div>'; 
     } // if ($posts 
    } // foreach($categories 
?> 

誰かが私に試してもらうために更新されたコードを提供できれば、それは非常に感謝しています。

+1

をしようと積極的に変更することができ、これをやりました!これに答える前の投稿があります:http://stackoverflow.com/questions/8643508/how-to-group-articles-by-tags/8645453#8645453 – CookiesForDevo

答えて

6

私はそれがすべての分類法を取得しますが、簡単にあなたはすでに、実際にはかなり密接しているもの、あなたが

// for a given post type, return all 
$post_type = 'shows'; 
$tax = 'show-topic'; 
$tax_terms = get_terms($tax, array('orderby' => 'id', 'order' => 'ASC', 'exclude' => '135, 49, 25, 24, 54')); 
if ($tax_terms) { 
    foreach ($tax_terms as $tax_term) { 
     $args = array(
      'post_type' => $post_type, 
      "$tax" => $tax_term->slug, 
      'post_status' => 'publish', 
      'posts_per_page' => - 1, 
      'orderby' => 'title', 
      'order' => 'ASC', 
      'caller_get_posts' => 1 
      ); // END $args 
     $my_query = null; 
     $my_query = new WP_Query($args); 
     if ($my_query->have_posts()) { 
      echo '<h3>' . $tax_term->name . '</h3>'; 
      while ($my_query->have_posts()) : $my_query->the_post(); 
      ?> 
      <div class="post row" id="post-<?php the_ID(); ?>"> 
        <div class="thumb-box three column"> 
         <?php 
      $src = wp_get_attachment_image_src(get_post_thumbnail_id()); 
      if (has_post_thumbnail()) { 
       the_post_thumbnail(); 
      } else { 
       if (get_post_meta($post->ID, "thumbnail", true)): 
        ?> 
           <a href="<?php the_permalink() ?>" rel="bookmark"><img src="<?php echo get_post_meta($post->ID, "thumbnail", true); ?>" alt="<?php the_title(); ?>" /></a> 
          <?php else: ?> 
           <a href="<?php the_permalink() ?>" rel="bookmark"><img src="<?php bloginfo('stylesheet_directory'); ?>/images/insp-tv-small.png" alt="<?php the_title(); ?>" /></a> 
          <?php endif; 
      } 
      ?> 
        </div> 
        <div class="post-content nine columns"> 
         <h4 class="posttitle archiveposttitle"> 
          <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php _e('Permanent Link to', 'buddypress') ?> <?php the_title_attribute(); ?>"><?php the_title(); ?></a> 
         </h4> 
         <div class="entry"> 
          <?php the_excerpt(); ?> 
         </div> 
        </div> 
       </div> 
      <?php 
      endwhile; 
     } // END if have_posts loop 
     wp_reset_query(); 
    } // END foreach $tax_terms 
} // END if $tax_terms 

?> 
関連する問題