2017-10-18 94 views

回答

0

嘗試這樣:

$categories = get_categories(array(
    'orderby' => 'name', 
    'parent' => 0, 
    'exclude' => 1 
)); 

遍歷

<?php foreach ($categories as $cat) { ?> 

<?php echo $cat->term_id; ?> 
<?php echo $cat->name; ?> 

    <?php } ?> 

然後

query_posts('post_type=post&post_status=publish&posts_per_page=3&cat=-1&paged='. get_query_var('paged')); 
+0

這將顯示所有的類別,甚至沒有被CPT'項目'使用的命令 – user759235

0

您應該保存所有的類時,你遍歷你的帖子在數組或您認爲有用的其他數據結構。

使用關聯數組將是一個簡單的解決方案。使用類別作爲鍵值和類別存檔URL作爲值,您將擁有創建每個類別鏈接所需的所有組件。

['category 1' => 'http://yoururl.com/category1',...]

foreach ($posts as $post) { 
    $category = get_the_category($post->ID); 
    ... 
    // using $category, get the title and URL for it 
} 

從這裏,你可以得到你需要爲每個類別的所有屬性。

+0

我需要這個在循環之外 – user759235

+0

然後你需要循環兩次'$ posts'。在第一個,獲取類別,在下一個,呈現帖子。 –

相關問題