php - 在 WordPress 中重新排序对象数组

标签 php arrays wordpress

我正在编写自定义 WordPress 脚本,该脚本应该在元素中显示所有自定义分类法。其中一些元素有子元素,另一些则没有。这是表单的代码:

                            <?php 
                            $terms = get_terms("location", "hide_empty=0");

                             $count = count($terms);
                             if ( $count > 0 ){
                                 foreach ( $terms as $term ) {
echo "<option value='" . $term->slug . "'>" . $term->name ."</option>";
                                 }
                             }

                        echo "</select>";

问题是,它按字母顺序显示所有元素,包括父元素和子元素。我希望 children 嵌套在 parent 下面,但我不知道如何做。有人可以提供一些帮助吗?

这是 $terms 数组的 print_r:

Array
(
[0] => stdClass Object
    (
        [term_id] => 18
        [name] => Andrijevica
        [slug] => andrijevica
        [term_group] => 0
        [term_taxonomy_id] => 18
        [taxonomy] => location
        [description] => 
        [parent] => 0
        [count] => 0
    )

[1] => stdClass Object
    (
        [term_id] => 19
        [name] => Berane
        [slug] => berane
        [term_group] => 0
        [term_taxonomy_id] => 19
        [taxonomy] => location
        [description] => 
        [parent] => 0
        [count] => 0
    )

[2] => stdClass Object
    (
        [term_id] => 17
        [name] => Bijelo Polje
        [slug] => bijelo-polje
        [term_group] => 0
        [term_taxonomy_id] => 17
        [taxonomy] => location
        [description] => 
        [parent] => 0
        [count] => 0
    )

.....
[29] => stdClass Object
    (
        [term_id] => 53
        [name] => Pobrežje
        [slug] => pobrezje
        [term_group] => 0
        [term_taxonomy_id] => 63
        [taxonomy] => location
        [description] => 
        [parent] => 4
        [count] => 0
    )

[30] => stdClass Object
    (
        [term_id] => 4
        [name] => Podgorica
        [slug] => podgorica
        [term_group] => 0
        [term_taxonomy_id] => 4
        [taxonomy] => location
        [description] => 
        [parent] => 0
        [count] => 7
    )

如您所见,parent 为 0。子项的父项值设置为父项的 term_id。例如,[30] 是 [29] 的父级。

最佳答案

使用get_term_children()在循环内。

示例:

$taxonomyName = "location"
$terms = get_terms($taxonomyName,array('parent' => 0));
foreach($terms as $term) {
    echo '<a href="'.get_term_link($term->slug,$taxonomyName).'">'.$term->name.'</a>';
    $term_children = get_term_children($term->term_id,$taxonomyName);
    echo '<ul>';
    foreach($term_children as $term_child_id) {
        $term_child = get_term_by('id',$term_child_id,$taxonomyName);
        echo '<li><a href="' . get_term_link( $term_child->name, $taxonomyName ) . '">' . $term_child->name . '</a></li>';
    }
    echo '</ul>';
}

抱歉,该示例是从我的一个项目中剪切和粘贴的,并将创建一个嵌套的 UL..如果您需要它作为下拉选项 - 好吧 - 我相信您可以根据您的需要修改它..

关于php - 在 WordPress 中重新排序对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20026821/

相关文章:

c - 用 C 中的数字对填充数组

wordpress - 如何阻止 Jetpack 从 WP.com 加载 CSS 文件?

php - 关于在php中保存文件的问题

javascript - 使用 jQuery 通过 Ajax 将数据从 javascript 发送到 php。如何查看回数据?

PHP 在 If/Else 中回显 MySQL 值

c - 在 Main 中动态分配结构体数组,然后为其分配函数

javascript - PHP 警报不显示

javascript - 如何通过过滤嵌套属性返回父对象

php - 购物车按钮的 woocommerce 代码

php - IE11 CSS动画问题