2009-11-22 103 views
7

我是新來的Wordpress,一直在努力創建一個類別循環的我的頭髮。循環應該:通過所有類別 循環通過wordpress類別

  • 回聲出類別名稱(與鏈接到
    1. 循環呼應了最後5帖在該類別中(與永久張貼)

    每個HTML代碼將是

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

    請幫助

  • +0

    是模板部分還是其他文件? – streetparade 2009-11-22 23:35:47

    回答

    6

    海蘭讓事情簡單在這裏是你如何解決這個問題

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

    是的,使用wp_list_categories並在設置 - 閱讀中,將您的「博客頁面最多顯示」設置爲5. – Michael 2009-11-23 03:10:07

    +0

    wp_list_categories()的唯一問題是您無法將其輸出控制在任何適當程度。 – 2014-10-14 14:01:55

    8

    哎呀,錯過了你想要5個員額

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

    我發現在頂部添加了內聯'global $ post;'需求,[https://codex.wordpress.org/Function_Reference/setup_postdata](https://codex.wordpress.org/Function_Reference/setup_postdata) – MrG 2013-10-11 15:52:18

    +0

    @MrG除非你在循環中使用它,是的。 – 2014-10-14 13:43:26

    1

    我已經通過嵌套由這段代碼循環類別。共享。

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