如何隐藏空子类别(WordPress)

时间:2018-02-28 09:47:24

标签: wordpress

我正在使用下面的代码显示空的子类别,其中没有任何帖子,我想隐藏这些。如果可能,请告诉我,如果是,那么

<?php
    $term = get_queried_object();
    $term_id = $term->term_id;
    $taxonomy_name = $term->taxonomy;

    $termchildren = get_term_children( $term_id, $taxonomy_name );

    echo '<ul>';
    foreach ( $termchildren as $child ) {
        $term = get_term_by( 'id', $child, $taxonomy_name );
        echo '<li><a href="' . get_term_link( $term, $taxonomy_name ) . '">' . $term->name.' ('. $term->count. ')</a></li>';
    }
    echo '</ul>';
?>

提前多多感谢

3 个答案:

答案 0 :(得分:0)

您可以使用$term->count检查是否有任何帖子。

foreach ( $termchildren as $child ) {
    $term = get_term_by( 'id', $child, $taxonomy_name );
    if($term->count > 0){
       echo '<li><a href="' . get_term_link( $term, $taxonomy_name ) . '">' . $term->name.' ('. $term->count. ')</a></li>';
    }
}

答案 1 :(得分:0)

我还有其他解决方案

$termchildren = get_terms( $taxonomy_name, array( 'hide_empty' => true, 'parent' => $term_id ) );

答案 2 :(得分:0)

您可以使用术语计数来检查空类别

if ( v == CLOCKWISE ) {
        if (random == 1) {
            new CountDownTimer(100,1) {
                @Override
                public void onTick(long millisUntilFinished) {
                    CLOCKWISE.setBackgroundColor(Color.GREEN);
                }
                @Override
                public void onFinish() {
                    CLOCKWISE.setBackgroundColor(Color.GRAY);
                    Animation clockwise = AnimationUtils.loadAnimation(NegativeActivity.this,R.anim.button_rotate_clockwise);
                    clockwise.setDuration(5000);
                    CIRCLE.startAnimation(clockwise);
                    Random r = new Random();
                    random = r.nextInt(11 - 1) + 1;
                    if(random == 1) {
                        textQueston.setText("Rotate the Circle Clockwise");
                    }
                    else if(random == 2) {
                        textQueston.setText("Make the Circle Blue");
                    }
                    else if(random == 3) {
                        textQueston.setText("Make the Circle Red");
                    }
                    else if(random == 4) {
                        textQueston.setText("Spin the Circle Anti-Clockwise");
                    }
                    else if(random == 5) {
                        textQueston.setText("Make the Circle Smaller");
                    }
                    else if(random == 6) {
                        textQueston.setText("Make the Circle Larger");
                    }
                    else if (random == 7) {
                        textQueston.setText("Rotate the Circle Faster Clockwise");
                    }
                    else if (random == 8) {
                        textQueston.setText("Rotate the Circle Slower Clockwise");
                    }
                    else if (random == 9) {
                        textQueston.setText("Rotate the Circle Faster Anti-Clockwise");
                    }
                    else if (random == 10) {
                        textQueston.setText("Rotate the Circle Slower Anti-Clockwise");
                    }
                }
            }.start();
        }
    }
    else if (v == ANTICLOCKWISE) {
        if (random == 4) {
            new CountDownTimer(100,1) {
                @Override
                public void onTick(long millisUntilFinished) {
                    ANTICLOCKWISE.setBackgroundColor(Color.GREEN);
                }
                @Override
                public void onFinish() {
                    ANTICLOCKWISE.setBackgroundColor(Color.GRAY);
                    Animation anti = AnimationUtils.loadAnimation(NegativeActivity.this,R.anim.button_rotate_anticlockwise);
                    anti.setDuration(5000);
                    CIRCLE.startAnimation(anti);
                    Random r = new Random();
                    random = r.nextInt(11 - 1) + 1;
                    if(random == 1) {
                        textQueston.setText("Rotate the Circle Clockwise");
                    }
                    else if(random == 2) {
                        textQueston.setText("Make the Circle Blue");
                    }
                    else if(random == 3) {
                        textQueston.setText("Make the Circle Red");
                    }
                    else if(random == 4) {
                        textQueston.setText("Spin the Circle Anti-Clockwise");
                    }
                    else if(random == 5) {
                        textQueston.setText("Make the Circle Smaller");
                    }
                    else if(random == 6) {
                        textQueston.setText("Make the Circle Larger");
                    }
                    else if (random == 7) {
                        textQueston.setText("Rotate the Circle Faster Clockwise");
                    }
                    else if (random == 8) {
                        textQueston.setText("Rotate the Circle Slower Clockwise");
                    }
                    else if (random == 9) {
                        textQueston.setText("Rotate the Circle Faster Anti-Clockwise");
                    }
                    else if (random == 10) {
                        textQueston.setText("Rotate the Circle Slower Anti-Clockwise");
                    }
                }
            }.start();
        }
    }