在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>";

问题是,它按字母顺序显示了所有元素,包括父母和孩子。我希望孩子被嵌套在父母的下方,但我不知道如何做。谁能提供帮助?

这是$ 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]的父级。

Obmerk Kronen

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 ..如果您需要它作为下拉选项-很好-我确信您可以根据需要对其进行修改。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

重新排列数组

来自分类Dev

重新排列numpy数组中的行条目

来自分类Dev

重新排列数组

来自分类Dev

重新排列javascript数组

来自分类Dev

重新排列单元格后如何更改fetchresultcontroller数组中对象的顺序

来自分类Dev

重新排列排序的数组

来自分类Dev

重新排列numpy数组

来自分类Dev

根据嵌套数组重新排列对象数组

来自分类Dev

在数组数组中重新排列值

来自分类Dev

合并多个对象与对象数组中值的总和,并在javascript中重新排列数组

来自分类Dev

重新排列JavaScript数组

来自分类Dev

在React JS中重新排列数组

来自分类Dev

重新排列WP_Post对象和数组的数组

来自分类Dev

在Simulink中重新排列数组

来自分类Dev

转换和重新排列数组中的范围操作

来自分类Dev

在numpy数组中重新排列子数组?

来自分类Dev

重新排列数组

来自分类Dev

重新排列SQLAlchemy Select对象中的列

来自分类Dev

如何重新排列数组

来自分类Dev

合并和重新排列两个对象数组

来自分类Dev

重新排列angularjs中的JSON对象以创建嵌套的ul

来自分类Dev

如何重新排列javascript数组中的对象?

来自分类Dev

重新排列numpy数组

来自分类Dev

在PHP中重新排列数组值

来自分类Dev

点击重新排列数组

来自分类Dev

使用嵌套对象(JS)重新排列数组

来自分类Dev

如何重新排列列表中的对象

来自分类Dev

重新排列对象中每隔两个键

来自分类Dev

在 ruby 中,如何重新排列具有 id 键的对象数组,将数组的新顺序作为数组?