2013-10-14 41 views
2

嗨,我是新來的wordpress,想知道如何只顯示父母的頂級孩子。我的意思是,讓我們說 電影是最重要的類別和孩子是迪斯尼,皮克斯等和迪斯尼的孩子是行動和皮克斯也是行動 在電影類我只想顯示迪士尼,皮克斯不行動 所以讓我們把它像這樣:只顯示父母的頂級孩子(Wordpress)

Movies - Main 
-Disney (Displayed in Movies Category) 
--Action NOT DISPLAYED 
-Pixar (Displayed in Movies Category) 
--Action NOT DISPLAYED 

但自動WordPress的顯示。我該如何解決這個問題?

+0

這可能是有益的http://stackoverflow.com /問題/ 16140145/WordPress的顯示可能只有父化類別的職位,沒有子類別,職位 – Shaheer

回答

0

爲此,你必須首先使用get_categories讓所有頂級類別,並獲取具有各自頂部類別作爲父的所有類別:

$args = array(
     'orderby' => 'name', 
     'parent' => 0 
    ); 
$top_categories = get_categories($args); 
foreach ($top_categories as $top_category) { 
    //here you display info about the top category or anything you want to do with it 
    echo '<a href="' . get_category_link($top_category->term_id) . '">' . $top_category->name . '</a><br/>'; 
    $args = array(
      'orderby' => 'name', 
      'parent' => $top_category->term_id 
     ); 
    $child_categories = get_categories($args); 
    foreach ($child_categories as $child_category) { 
    //here you write the code for child categories 
     echo '<a href="' . get_category_link($child_category->term_id) . '">' . $child_category->name . '</a><br/>'; 
    } 
}