posts - get_the_category listing in hierarchial order

Hello at the moment I'm wondering how I would be able to make this work to post in Hierarchical order within the po

Hello at the moment I'm wondering how I would be able to make this work to post in Hierarchical order within the post loop.

<?php 
    foreach ((get_the_category()) as $childcat) 
    { 
        if (cat_is_ancestor_of('42', $childcat)) 
        { 
            echo '<li> <a href="'.get_category_link($childcat->cat_ID).'">'; echo $childcat->cat_name . '</a> </li>'; 
        }
    }
?>

I would like it to output as such:

  • Parent
    • Child
    • Child
      • Grandchild

Hello at the moment I'm wondering how I would be able to make this work to post in Hierarchical order within the post loop.

<?php 
    foreach ((get_the_category()) as $childcat) 
    { 
        if (cat_is_ancestor_of('42', $childcat)) 
        { 
            echo '<li> <a href="'.get_category_link($childcat->cat_ID).'">'; echo $childcat->cat_name . '</a> </li>'; 
        }
    }
?>

I would like it to output as such:

  • Parent
    • Child
    • Child
      • Grandchild
Share Improve this question edited Aug 5, 2012 at 15:16 Amit Kosti 3,6101 gold badge22 silver badges34 bronze badges asked Jun 22, 2012 at 18:54 espnicholasespnicholas 1794 silver badges11 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 2

Use wp_list_categories instead,

<?php 

    $args = array(
    'hierarchical'       => true,
    'child_of'            => 42,   //parent category
    'hide_empty'         => 1,    //hide empty categories (set to 0 to show)
    );

    wp_list_categories($args); 

?> 

A full list of parameters for wp_list_categories can be found HERE

As you can see its highly customizable simply by adding extra arguements to the array stored in the $args variable that we later pass to the wp_list_categories function.

Note: Adding the child_of => ID argument you can specify the parent ID of the category for which you want to retrieve category terms for. Be aware that if there are no posts in the parent category, it will not show, in which case you may try setting hide_empty => 0 instead.

UPDATE

Well, instead of writing this from scratch myself, Scribu already has thankfully, so please take a look at the following link for more details;

http://scribu/wordpress/extending-the-category-walker.html

You need to sort the categories to make sure they come in the correct order:

// get categories of post in sorted order
$categories = sortCategories(get_the_category());     

function sortCategories($categories) { // Sorting the category
    usort($categories, "cmpCategories");
    return $categories;
}

function cmpCategories($category_1,$category_2) { // Sort function
    foreach(get_categories(array("parent" => $category_1->cat_ID)) AS $sub) {
        if($category_2->cat_ID == $sub->cat_ID) return -1;
    }
    return 1;
}

In addition to @tobidude's great solution, a complete exemple:

Create the functions

function sortCategories($categories) { // Sorting the category
    usort($categories, "cmpCategories");
    return $categories;
}

function cmpCategories($category_1,$category_2) { // Sort function
    foreach(get_categories(array("parent" => $category_1->cat_ID)) AS $sub) {
        if($category_2->cat_ID == $sub->cat_ID) return -1;
    }
    return 1;
}

Call your WP_Query... and during the loop call the get_the_category() inside the function sortCategories:

$sub_cat = sortCategories(get_the_category()); 

Before print the category, I created a conditional to get the deepest category:

if(isset($sub_cat[2])):
  echo $sub_cat[2]->name;
elseif(isset($sub_cat[1])):
  echo $sub_cat[1]->name;
else:
  echo $sub_cat[0]->name;
endif;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745270730a4619729.html

相关推荐

  • posts - get_the_category listing in hierarchial order

    Hello at the moment I'm wondering how I would be able to make this work to post in Hierarchical order within the po

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信