2010-07-16 25 views
5

tôi có một danh mục có tên là tin tức và nhiều tiểu thể loại bên trong nó. Điều tôi muốn làm là chỉ nhận được 1 bài đăng (mới nhất) từ mỗi danh mục phụ (bao gồm tiêu đề danh mục, tiêu đề bài đăng, hình ảnh đính kèm). Có đề xuất bạn bè nào không ??làm cách nào để tôi chỉ nhận được 1 bài đăng từ mỗi thể loại trong wordpress

+3

Đây là câu hỏi thứ 6 của bạn về SO và bạn vẫn chưa chấp nhận bất kỳ câu trả lời nào trước đó. Xin vui lòng làm như vậy nếu có bất kỳ câu trả lời hữu ích, điều này sẽ cải thiện nghiệp của bạn ở đây :) – FelipeAls

Trả lời

11
<?php 

$news_cat_ID = get_cat_ID('News'); 
$news_cats = get_categories("parent=$news_cat_ID"); 
$news_query = new WP_Query; 

foreach ($news_cats as $news_cat) : 
    $news_query->query(array(
     'cat'     => $news_cat->term_id, 
     'posts_per_page'  => 1, 
     'no_found_rows'  => true, 
     'ignore_sticky_posts' => true, 
    )); 

    ?> 

    <h2><?php echo esc_html($news_cat->name) ?></h2> 

    <?php while ($news_query->have_posts()) : $news_query->the_post() ?> 

      <div class="post"> 
       <?php the_title() ?> 
       <!-- do whatever you else you want that you can do in a normal loop --> 
      </div> 

    <?php endwhile ?> 

<?php endforeach ?> 
+0

cảm ơn u THEDeadMedic. nó hoạt động, và bạn có thể giúp tôi một cái khác không. (mở rộng cùng mã) làm cách nào để chỉ hiển thị những bài đăng có đính kèm hình ảnh và được sắp xếp theo ngày – sonill

+0

cảm ơn vì điều này, đang tìm kiếm chính xác điều tương tự! +1 – Tarun

+0

Đã dành cho tôi rất nhiều thời gian, cảm ơn @ TheDeadMedic –

0

Sau một vài giờ và nhờ các nghiên cứu sinh trên toàn cầu, tôi đã có thể sửa đổi truy vấn chính, vì vậy chúng tôi thậm chí không cần phải gửi mẫu và tạo truy vấn và vòng lặp mới .. .

// My function to modify the main query object 
function grouped_by_taxonomy_main_query($query) { 

    if ($query->is_home() && $query->is_main_query()) { // Run only on the homepage 

     $post_ids = array(); 

     $terms = get_terms('formato'); 

     foreach ($terms as $term) { 
      $post_ids = array_merge($post_ids, get_posts(array( 
       'posts_per_page' => 4, // as you wish... 
       'post_type' => 'video', // If needed... Default is posts 
       'fields' => 'ids', // we only want the ids to use later in 'post__in' 
       'tax_query' => array(array('taxonomy' => $term->taxonomy, 'field' => 'term_id', 'terms' => $term->term_id,)))) // getting posts in the current term 
      ); 
     } 

     $query->query_vars['post_type'] = 'video'; // Again, if needed... Default is posts 
     $query->query_vars['posts_per_page'] = 16; // If needed... 
     $query->query_vars['post__in'] = $post_ids; // Filtering with the post ids we've obtained above 
     $query->query_vars['orderby'] = 'post__in'; // Here we keep the order we generated in the terms loop 
     $query->query_vars['ignore_sticky_posts'] = 1; // If you dont want your sticky posts to change the order 

    } 
} 

// Hook my above function to the pre_get_posts action 
add_action('pre_get_posts', 'grouped_by_taxonomy_main_query'); 
Các vấn đề liên quan