【问题标题】:All custom taxonomy classes not showing despite hide_empty being false尽管 hide_empty 为 false,但所有自定义分类类均未显示
【发布时间】:2018-01-22 20:44:33
【问题描述】:

我创建了一个查询,该查询应列出自定义分类“类”中的所有术语。我知道默认情况下它不会得到空的术语,所以我添加了 hide_empty' => false。

它仍然不会检索那些为空的术语。我意识到我一定在这里做错了,需要在某个地方再次添加 hide_empty' => false

有什么想法吗?

<?php
$classes = get_terms( array(
    'taxonomy' => 'classes',
    'hide_empty' => false

) );

if ( $classes ) {
    foreach ( $classes as $class ) {
        $terms = get_terms( $class );
        foreach ( $terms as $term ) {
            if ( $term->parent != 0 ) {
                ?>

            <p><?php echo $term->name; ?></p>

                <?php
            }
        }
    }
}
?>

【问题讨论】:

    标签: php wordpress taxonomy-terms


    【解决方案1】:

    不明白为什么在启动 foreach 后再次调用 $terms = get_terms( $class );,尝试回显它而不再次调用它。请尝试以下操作:

    <?php
    $classes = get_terms( array(
        'taxonomy' => 'classes',
        'hide_empty' => false
    
    ) );
    
    if ( $classes ) {
        foreach ( $classes as $term) {
                if ( $term->parent != 0 ) {
                    ?>
    
                <p><?php echo $term->name; ?></p>
    
                    <?php
                }
         }
    
     }
    ?>
    

    您还可以通过转储返回的变量来调试代码,如下所示,以了解问题出在哪里:

    <?php
    $classes = get_terms( array(
        'taxonomy' => 'classes',
        'hide_empty' => false
    
    ) );
    
    if ( $classes ) {
        foreach ( $classes as $class ) {
            $terms = get_terms( $class );
            var_dump($terms); //It's probably going to be 'false'
            foreach ( $terms as $term ) {
                var_dump($term);
                if ( $term->parent != 0 ) {
                    ?>
    
                <p><?php echo $term->name; ?></p>
    
                    <?php
                }
            }
        }
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-07
      • 2021-06-24
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      • 2012-03-19
      相关资源
      最近更新 更多