2017-08-17 97 views
0

我是wordpress開發新手。我想知道如何在wordpress的特定頁面上顯示特定類別。我需要在我的項目中做到這一點。如何僅顯示Wordpress中特定頁面上的特定類別?

+0

歡迎堆棧溢出。請注意,不鼓勵提出一般性幫助或建議的問題:請參閱[允許的主題](https://stackoverflow.com/help/on-topic)。這不是一個代碼寫作服務,你需要研究你的問題,並試圖在發佈之前解決它。請閱讀[Stack Overflow用戶需要多少研究工作?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users)和[我如何問一個好問題](https://stackoverflow.com/help/how-to-ask) – FluffyKitten

回答

0

顯示作爲鏈接的類別列表。

基本上,您必須在要查看列出的類別的位置調用此函數wp_list_categories();

並使用此選項include接受要顯示的類別ID列表。

喜歡這張

$args = array(
    'hide_empty' => 0, //Show me all the categories, even the empty ones 
    'orderby' => 'count', //which accepts a string. You can pick from the following options: ID to order the categories by their ID (no, really?), name to sort them alphabetically (the default value), slug to sort them in the alphabetical order of their slugs, and count to order by the number of posts they contain. 
    'order' => 'DESC', //The chosen order can be reversed by setting DESC (descending) as a value for the order option (by default this option is set to ASC (ascending)). 
    'include' => '15,16,9' 
); 

wp_list_categories($args); 

另一種方式使用get_categories();功能

$categories = get_categories(array(
    'orderby' => 'name', 
    'order' => 'ASC', 
    'include' => '15,16,9' 
)); 

foreach($categories as $category) { 
    $category_link = sprintf( 
     '<a href="%1$s" alt="%2$s">%3$s</a>', 
     esc_url(get_category_link($category->term_id)), 
     esc_attr(sprintf(__('View all posts in %s', 'textdomain'), $category->name)), 
     esc_html($category->name) 
    ); 

    echo '<p>' . sprintf(esc_html__('Category: %s', 'textdomain'), $category_link) . '</p> '; 
    echo '<p>' . sprintf(esc_html__('Description: %s', 'textdomain'), $category->description) . '</p>'; 
    echo '<p>' . sprintf(esc_html__('Post Count: %s', 'textdomain'), $category->count) . '</p>'; 
} 

差異與get_categories之間wp_list_categories WordPress的功能get_categories()wp_list_categories()幾乎顯示類別噸他一樣。他們使用幾乎相同的參數,但它們之間存在差異:

get_categories()函數返回一個匹配查詢參數的類別對象數組。 wp_list_categories()顯示鏈接的類別列表。 WordPress的開發機制的文檔:

get_categories():http://codex.wordpress.org/Function_Reference/get_categories

wp_list_categories():http://codex.wordpress.org/Function_Reference/wp_list_categories

相關問題