2012-05-08 3 views
1

오랜 질문 제목에 대해 유감스럽게 생각합니다. 나는 정확한 것을 시도하고 있었다.WordPress - 각 카테고리가 DIV에 자동으로 포함 된 사용자 정의 게시물 유형 및 카테고리 별 게시물 받기

특정 사용자 정의 게시 유형에서 자동으로 게시물을 가져 와서 각 게시물이 분류되는 방식을 감지 한 다음 각 범주가 페이지에 범주별로 게시물을 출력하는 WordPress 쿼리를 고안해야합니다 (각 범주는 자체 DIV로 묶여 있음). .

예를 들어, "지도 데이터"라는 사용자 정의 게시 유형이 있습니다. 이 사용자 정의 게시 유형 내에서 필자는 "카테고리"라는 이름의 계층 적 분류를 가지고 있으며 해당 분류 내에서 "카테고리 # 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> 

I 기본 워드 프레스 카테고리 시스템과 함께 작동하는 다음 코드가 그러나, 나는 그것들을 다시 쓰거나, 사용자 정의 포스트 유형과 그들의 택 소노 미와 함께 작동 할 수 있도록 업데이트 할 필요가있다.

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

?> 
관련 문제