0

A template I'm using is calling the_category() to list the categories which a post belongs. It's ordering them by name. Is there a way to reverse the order by ID category?

4

2 回答 2

4

the_category(' ')用这个替换你的

$cats=get_the_category();
$cid=array();
foreach($cats as $cat)  { $cid[]=$cat->cat_ID; }
    $cid=implode(',', $cid);
    foreach((get_categories('orderby=name&include='.$cid)) as $category) { // notice orderby
        echo '<a href="'.get_category_link($category->cat_ID).'">'.$category->cat_name.'</a> '; // keep a space after </a> as seperator 
    }

您可以使用orderby值以下之一

id
name - default
slug
count
term_group
于 2012-04-22T23:12:58.757 回答
0

目前 the_category 没有按功能排序。你需要去wp-includes/category-template.php该文件中有一个名为“get_the_terms”的函数。在这个函数中有一行像$terms = wp_get_object_terms( $id, $taxonomy );

wp_get_object_terms 可以为 args 提供额外的参数。您可以在此处编辑,如下所示;

$args = array('orderby' => 'id', 'order' => 'ASC');
$terms = wp_get_object_terms( $id, $taxonomy, $args);

您可以根据需要修改 $args

希望这可以帮助

于 2012-04-22T21:01:11.767 回答