使用get_categories复制wp_list_categories

时间:2015-01-26 15:55:01

标签: php wordpress

提前致谢。

我已经搜索过高低的解决方案,但却无法找到它,当然它比我想象的更简单?基本上我需要复制wp_list_categories的输出并更改<a href="">以包含子类别的数据过滤器。我可以在没有父类别问题的情况下执行此操作,但我似乎也无法输出子类别。

有没有办法甚至可以将数据过滤器添加到输出的<a href="">而不是复制整个输出?

这是我目前为止的代码,它只输出父类别。

<ul id="ondemandNav">
                    <?php $args = array(
                        'taxonomy' => 'categoriestest',
                        'parent' => 0,
                        'hide_empty' => 0
                    );

                    $categories = get_categories($args);
                    $catid = array();
                     foreach($categories as $category)  {

                         echo '<li class="parent-item"><a href="">' . $category->name . '</a> .';

                         echo '</li>';
                         array_push($catid, $category->term_id);

                    } ?>
                </ul>

有一种简单的方法吗?

1 个答案:

答案 0 :(得分:0)

看起来你需要第二个循环来接孩子和孙子。我实际上没有尝试过这段代码,但它看起来应该符合get_categories() Codex页面。

http://codex.wordpress.org/Function_Reference/get_categories

<ul id="ondemandNav">
<?php $args = array('taxonomy'   => 'categoriestest',
                    'parent'     => 0,
                    'hide_empty' => 0 );

$categories = get_categories($args);
$catid = array();

foreach($categories as $category)  {
    $child_args = array('child_of'=> $category->term_id); 
    $child_categories = get_categories($args);
    echo '<li class="parent-item"><a href="">' . $category->name . '</a> .</li>';

    foreach ($child_categories as $child_category) {
        echo '<li class="parent-item"><a href="">' . $child_category->name . '</a> .</li>';

    }
}
?>
</ul>

因为它说它取了孩子孙子,所以我猜你不需要递归方法一直钻到底部。

HTH,

= C =

相关问题