2009-11-22 37 views
7

Tôi mới dùng Wordpress và đang kéo tóc ra cố gắng tạo vòng lặp danh mục. Vòng lặp có nghĩa vụ phải:Lặp qua các loại wordpress

  1. lặp qua tất cả các loại
  2. echo ra tên loại (với liên kết đến
  3. echo ra 5 bài viết cuối cùng trong danh mục đó (với permalink để gửi)

Các html cho mỗi sẽ

<div class="cat_wrap"> 
    <div class="cat_name"> 
     <a href="<?php get_category_link($category_id); ?>">Cat Name</a> 
    </div> 
    <ul class="cat_items"> 
     <li class="cat_item"> 
     <a href="permalink">cat item 1</a> 
     </li> 
     <li class="cat_item"> 
     <a href="permalink">cat item 2</a> 
     </li> 
     <li class="cat_item"> 
      <a href="permalink">cat item 3</a> 
     </li> 
     <li class="cat_item"> 
     <a href="permalink">cat item 4</a> 
     </li> 
     <li class="cat_item"> 
     <a href="permalink">cat item 5</a> 
     </li> 
    </ul> 
</div> 

Xin giúp

+0

là phần mẫu hoặc các tệp khác? – streetparade

Trả lời

6

Hy giữ những điều đơn giản ở đây là làm thế nào bạn có thể giải quyết nó

<?php wp_list_categories('show_count=1&title_li=<h2>Categories</h2>'); ?> 
+1

Đúng, sử dụng wp_list_categories và trong Cài đặt-Đọc, hãy đặt "các trang Blog hiển thị nhiều nhất" thành 5. – Michael

+0

Vấn đề duy nhất với wp_list_categories() là bạn không thể kiểm soát đầu ra của nó ở bất kỳ mức độ nào. –

8

Rất tiếc, nhỡ mà bạn muốn 5 bài viết

<?php 
//for each category, show 5 posts 
$cat_args=array(
    'orderby' => 'name', 
    'order' => 'ASC' 
    ); 
$categories=get_categories($cat_args); 
    foreach($categories as $category) { 
    $args=array(
     'showposts' => 5, 
     'category__in' => array($category->term_id), 
     'caller_get_posts'=>1 
    ); 
    $posts=get_posts($args); 
     if ($posts) { 
     echo '<p>Category: <a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . '>' . $category->name.'</a> </p> '; 
     foreach($posts as $post) { 
      setup_postdata($post); ?> 
      <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p> 
      <?php 
     } // foreach($posts 
     } // if ($posts 
    } // foreach($categories 
?> 
+0

Tôi thấy rằng nội tuyến 'toàn cầu $ post;' cần thêm vào điều này ở trên cùng, [https://codex.wordpress.org/Function_Reference/setup_postdata](https://codex.wordpress.org/Function_Reference/setup_postdata) – MrG

+0

@MrG Trừ khi bạn đang sử dụng nó trong vòng lặp, có. –

1

Tôi đã thực hiện chút mã này để lặp qua lồng nhau Thể loại. Chia sẻ.

 //Start on the category of your choice  
     ShowCategories(0); 

     function ShowCategories($parent_category) { 
       $categories = get_categories(array('parent' => $parent_category, 'hide_empty' => 0)); 
       foreach ($categories as $category) { 
        ?><ul><li><?=$category->cat_name;?><? 
        ShowCategories($category->cat_ID); 
        ?></li></ul><? 
       } 
     } 
0

Hãy xem chủ đề này Stackoverflow khác:

https://wordpress.stackexchange.com/questions/346/loop-through-custom-taxonomies-and-display-posts/233948#233948

tôi đã đăng một câu trả lời mà tôi sử dụng trong sản xuất và hoạt động giống như một nét duyên dáng.

Chỉ cần nhớ điều chỉnh các đối số để chỉ hiển thị 5 bài đăng, thay vì tất cả.

$args = array('showposts' => 5); 

Thêm 'showposts' => 5 vào mảng đối số hiện tại của bạn trong vòng lặp lặp qua bài đăng của từng danh mục.

Các vấn đề liên quan