加载按重量排序的术语

问题描述 投票:2回答:1

当根据术语权重从升序中加载术语时,是否有人能指出正确的方向?

这就是我现在拥有的:

$taxonomy = taxonomy_vocabulary_machine_name_load('tool_type');
$terms = entity_load('taxonomy_term', FALSE, array('vid' => $taxonomy->vid));

从我所看到的,我的术语现在按字母顺序排序,这不是我需要的。

我还发现,当你可以拖放CMS中的术语来改变术语的权重时,字母顺序似乎是标准的。如果这些术语按重量分类为标准则更有意义。任何人都可以向我解释为什么这样实现?

sorting drupal drupal-7 drupal-taxonomy
1个回答
0
投票

您可以使用分类项的weight属性对它们进行排序:

$taxonomy = taxonomy_vocabulary_machine_name_load('tool_type');
$terms = entity_load('taxonomy_term', FALSE, array('vid' => $taxonomy->vid));
uasort($terms, function($a, $b) {
    if ($a->weight == $b->weight) return strcmp($a->name, $b->name) ;
    return $a->weight > $b->weight;
});

这将按重量排序。如果重量相同,则按名称排序。

© www.soinside.com 2019 - 2024. All rights reserved.