2013-04-02 123 views
2

任何人都可以請告訴我如何顯示我的wordpress類別(父母和孩子)以及他們的ID。 我希望他們以這種方式印刷:如何顯示wordpress類別列表與他們的ID

歐洲,英國,倫敦(歐洲是一個父類和他們的孩子的英國) 10,20,33(這些都是他們IDS)

歐洲,法國巴黎 10,22,45

歐洲,法國,戛納 10,22,49

我想這個代碼,但它並沒有爲我工作:

 <?php 
$categories = get_the_category(); 
$this_cat_ID = $categories[0]->cat_ID; 
$this_cat_name = $categories[0]->cat_name; 
$this_cat_url = get_category_link($this_cat_ID); 
// get the sub category if we have them 
foreach ($categories as $cat) { 
    $parent = $cat->category_parent; 
    if ($parent != 0){ 
     $sub_cat_ID = $cat->cat_ID; 
     $sub_cat_name = $cat->cat_name; 
     $sub_cat_url = get_category_link($sub_cat_ID); 
    } 
} 
if (!$sub_cat_ID) { 
    echo $this_cat_ID; 
} else { 
    echo $sub_cat_ID; 
} 
?> 

非常感謝您的幫助

+0

你試過了什麼?請閱讀** [FAQ](http://stackoverflow.com/faq)**瞭解如何提出問題。 – brbcoding

+0

你想要所有的類別還是隻需要與特定帖子相關的類別。 –

+0

彼得,我想要我所有的類別和他們的孩子的id和孩子的孩子。謝謝 –

回答

3

WordPress的功能wp_list_categories將返回所有類別的列表。如果您將分層標誌設置爲true,您將獲得整個層次結構。有關詳細信息,請閱讀上面鏈接中的codex文章。

還有一個get_categories function返回一個未格式化的結果。你可以在你自己的PHP代碼中使用它。

第三種選擇是讀取數據庫,有三個表wp_terms,wp_term_taxonomy和wp_term_relationships包含類別樹。這是the database structure

編輯:這是一個將產生類似的列表作爲列表的嵌套集合的簡碼:

function show_categories($atts, $content) { 
    extract(shortcode_atts(array('taxonomy' => 'category'), $atts)); 
    $cats = get_categories(array('taxonomy' => $taxonomy,'hide_empty' => 0, 'hierarchical' => 0, 'parent' => 0)); 
    return show_categories_level($cats, '', '', $taxonomy); 
} 

function show_categories_level($cats, $names, $ids,$taxonomy) { 
    $res = '<ul>'; 
    foreach ($cats as $cat) { 
     if($names)$n = "$names, $cat->name"; else $n = $cat->name; 
     if($ids)$i = "$ids, $cat->term_id"; else $i = $cat->term_id;  
     $res = $res."<li>$n : $i</li>"; 
     $kittens = get_categories(array('taxonomy' => $taxonomy,'hide_empty' => 0, 'hierarchical' => 0, 'parent' =>$cat->term_id)); 
     if($kittens) { 
      $res .= ("<li>".show_categories_level($kittens, $n, $i, $taxonomy)."</li>"); 
     } 
    } 
    return $res."</ul>"; 
} 
add_shortcode('show-categories', 'show_categories'); 

要使用此此代碼添加到您的functions.php並添加簡碼,無論你想讓它顯示:

<h2>Default Categories</h2> 
[show-categories] 

,或者您可以指定要列出一個分類

<h2>My Taxonomy Categories</h2> 
[show-categories taxonomy="my_taxonomy"] 

這不是獲得此結果的最有效方式,但它在此處起作用。如果您從get_categories的分層版本開始,或者使用數據庫,則可以獲得更快的版本。

+0

感謝彼得的快速回復,你可以告訴我的代碼,因爲我不擅長PHP。 –

+0

恐怕先生,我真的很感激你的好意,並試圖幫助我,但代碼對我不起作用。實際上,我將第一個代碼添加到了functions.php並將簡碼添加到了catlist.php。然而,我所能看到的是:** Shopp Categories [show-categories taxonomy =「shopp_category」] 默認分類 [顯示類別] ** –

+0

如果您在頁面上看到[],表示短代碼不工作。你可以檢查安裝它們的代碼(add_shortcode)是否實際運行? –

1

你想在哪顯示?內循環?外?在single.php? category.php? .....?

如果要在獨立位置顯示它,請首先包含wp-load。PHP,這樣

<? php 


header('Content-Type: text/html; charset: UTF-8'); 
require('../../../../wp-load.php'); // use the path which will fit your situation 

$my_query = new WP_Query(); 
$my_query->query(array(
      'post_type' => 'post', 
      'orderby' => 'title', 
      'order'  => 'ASC', 
     )); 

if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post(); 

// use your code here after checking codex 

endwhile; 
endif; 
?> 
+0

如果你仍然不知道如何使用這個,我保證明天給完整的代碼源。但我建議挖掘它來學習。 WordPress是一個很有用的世界。 a + –

+0

如此友善的信函和空白處,我不擅長php,如果你能給出完整的代碼來幫助我,我會感激你。 –

+0

你在哪裏?你沒有回答我的問題!你想在哪裏顯示這個?內循環?外?在single.php? category.php? .....?在一個單獨的自定義地方? –

0

什麼我想要做的是這樣的: The steps I Followed(請查看此鏈接)

我只是想改變這種代碼:

<ul> <?php wp_list_categories('show_count=1&title_li=&hide_empty=0'); ?></ul> 

以便列表的所有類別及其孩子將以其格式顯示在此格式中:

例如: 歐洲,英國,倫敦,東漢姆:25,28,34,36 這只是一個父類別(歐洲)及其子女(英國) 英國的孩子是倫敦,而倫敦的孩子是東火腿。 25是歐洲的id,28是英國的id,34是倫敦的id,36是東部火腿的id,,上面的鏈接。 非常感謝你

+0

我已經在我的答案中添加了一些代碼,可以生成像您要求的列表。 –