如果有子项,则将IF函数添加到当前自定义类别

时间:2016-10-24 作者:Danb91

我想添加一个if语句,该代码回显当前类别的子类别,但我只想显示子类别是否为true。

任何帮助都会很好-谢谢你。

<?php (
$terms = get_terms([
    \'taxonomy\' => get_queried_object()->taxonomy,
    \'parent\'   => get_queried_object_id(),
]));
echo \'<div style="height: 200px; text-transform: uppercase; border:1px solid #666666; padding:10px; overflow-y: scroll;">
<div class="breaker-small">Refine Search</div>\';
foreach ( $terms as $term) {
    echo \'<p class="filters"><a href="\' . get_term_link( $term ) . \'">\' . $term->name . \'</a></p>\';  
}
echo \'</div>
<br />\';
?>

1 个回复
最合适的回答,由SO网友:AddWeb Solution Pvt Ltd 整理而成

如果你问起$terms 有了孩子就只能去追求了。使用此选项,您可以尝试以下代码作为解决方案:

<?php (
    $terms = get_terms([
        \'taxonomy\' => get_queried_object()->taxonomy,
        \'parent\'   => get_queried_object_id(),
        \'hide_empty\' => false
    ]));
    // get_terms will return false if taxonomy does not exist or term wasn\'t found.
    // term has children
    if($terms){
        echo \'<div style="height: 200px; text-transform: uppercase; border:1px solid #666666; padding:10px; overflow-y: scroll;">
                        <div class="breaker-small">Refine Search</div>\';
                         foreach ( $terms as $term) {
                            echo \'<p class="filters"><a href="\' . get_term_link( $term ) . \'">\' . $term->name . \'</a></p>\';  
                         }
        echo \'</div><br />\';
    }
?>
我希望我的这个代码能为你工作!