2015-11-02 4 views
1

Я хочу перечислить все подкатегории для определенной родительской категории в WordPress, я новичок в PHP, поэтому мне нужна помощь, любая помощь будет оценена ралли, мне нужно что-то вроде кодаСписок всех подкатегорий родительской категории

<h2>Maine Category 1</h2> 

<div class="row"> 
<div class="col">Subcategory 1</div> 
<div class="col">Subcategory 2</div> 
</div> 

<div class="row"> 
<div class="col">Subcategory 3</div> 
<div class="col">Subcategory 4</div> 
</div> 

<div class="row"> 
<div class="col">Subcategory 5</div> 
<div class="col">Subcategory 6</div> 
</div> 

ответ

0

Попробуйте это
ПРИМЕЧАНИЕ: 15 является идентификатором вашей родительской категории.

$args = array('child_of' => 15); 
$categories = get_categories($args); 
foreach($categories as $category) { 
    echo '<p>Category: <a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . '>' . $category->name.'</a> </p> '; 
    echo '<p> Description:'. $category->description . '</p>'; 
    echo '<p> Post Count: '. $category->count . '</p>'; 
} 
0

Вы можете использовать wp_list_categories.

Пример:

$parentcat = $i;//category id wp_list_categories("depth=2&child_of=" . $parentcat . "&title_li=");

или вы можете попробовать это в другом процессе.

<?php 
$cat_id = get_query_var('cat'); 

if(! empty($cat_id)) { 
    $category = get_category($cat_id, ARRAY_A); 

    if(! empty($category)) { 
    // parent category 
    if($category['parent'] === '0') { 

     // get child IDs 
     $terms = get_term_children($cat_id, 'category'); 

     foreach($terms as $term_id) { 
      $child_category = get_term($term_id, 'category'); 

      // if you need the category 
      $category_name = $child_category->name; 




      // do whatever you like with the categories now... 
      echo '<p>Category: <a href="' . get_category_link($child_category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $child_category->name) . '" ' . '>' . $child_category->name.'</a> </p> '; 
    } 

} else { 
// in child category 
    // do the regular loop here, if (have_posts()) ... 
    } 
} 
} 
?> 

также он отправил Here

надеюсь, что это может помочь вам :)

+0

Привет Lemon, спасибо за быстрый ответ, позвольте мне попробовать с этим кодом. – Sajal

Смежные вопросы