2012-11-02 46 views
8

我有我自己的主題,我想從特定類別在我的主頁上顯示帖子。如何使用slu get從類別中獲取帖子?

到目前爲止,我已經這樣實現的:

<?php 
    global $post; 
    $args = array('numberposts' => 10, 'category' => 6); 
    $posts = get_posts($args); 
    foreach($posts as $post): setup_postdata($post); 
?> 

    <divs with the_title() the_excerpt() etc ></div> 

<?php 
    endforeach; 
?> 

但是,如果我想獲得由它的蛞蝓種類是什麼?或者可以在管理面板中簡單地創建類別選擇框?

回答

24

與CATEGORY_NAME

global $post; 
$args = array('numberposts' => 10, 'category_name' => 'cat-slug'); 
$posts = get_posts($args); 
foreach($posts as $post): setup_postdata($post); 

?> 

<divs with the_title() the_excerpt() etc ></div> 

<?php 

endforeach; 

取代你的類參數的詳細信息檢查此鏈接http://codex.wordpress.org/Class_Reference/WP_Query#Parameters

+0

@IoQ如何使用此代碼分頁? – Amin

+0

非常感謝,你爲我節省了時間 – Bellash

2

假設你有類別名稱「冰蛋糕」和類別蛞蝓爲「冰蛋糕」,那麼我們的代碼檢索類別'冰糕'下的帖子如下:

<?php 
       $args = array('posts_per_page' => 3, 
       'category_name' => 'ice-cakes'); 

       $icecakes = get_posts($args); 
       foreach ($icecakes as $post) : setup_postdata($post); ?> 
        <li> 
         <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
        </li> 
       <?php endforeach; 
       wp_reset_postdata(); ?>