WordPress 分类排序:按自定义字段 'cat_priority' 排序
为了按照 "cat_priority" 自定义字段排序分类,您可以修改代码如下:
<ul>
<?php
$parent_categories = get_categories(array(
'parent' => 0,
'hide_empty' => false, // 设置为 true 以隐藏空分类
'meta_key' => 'cat_priority', // 按 "cat_priority" 自定义字段排序
'orderby' => 'meta_value_num', // 按数字值排序
'order' => 'ASC' // 按升序排序
));
foreach ($parent_categories as $category) {
$child_categories = get_categories(array(
'parent' => $category->term_id,
'hide_empty' => false
)); ?>
<li <?php if (!empty($child_categories)) { echo 'class="has_sub"'; } ?>>
<a><span><?php echo $category->name; ?></span></a>
<?php if (!empty($child_categories)) { ?>
<ul class="children">
<?php foreach ($child_categories as $child_category) { ?>
<li><a><span><?php echo $child_category->name; ?></span></a></li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } ?>
</ul>
这段代码在 get_categories 函数中使用 meta_key, orderby 和 order 参数,按升序顺序,根据 "cat_priority" 自定义字段对分类进行排序。请确保将 "cat_priority" 替换为您实际的自定义字段名称。
原文地址: https://www.cveoy.top/t/topic/nen9 著作权归作者所有。请勿转载和采集!