首先,我不擅长PHP,所以这可能是问题所在。。。因此,在尝试做我想做的事情但没有成功之后,我想寻求我们的帮助。对你们大多数人来说,这可能就像吃早餐一样简单。
我有一段代码,仅当子项不为空时才显示它们,我想将其更改为显示子项,即使它们为空。这是Adifier WP主题的一部分。
<?php
if( !empty( $term->children ) ){
?>
<ul class="list-unstyled hidden">
<?php self::taxonomy_listing( $name, $term->children, $taxonomy, $selected_term ); ?>
</ul>
<?php
}
?>
这是一个完整的功能代码。也许这也会有帮助:
/*
* Display filter taxonomies
*/
static public function taxonomy_listing( $name, $terms, $taxonomy, $selected_term ){
$search_more_less = adifier_get_option( \'search_more_less\' );
$counter = 0;
foreach( $terms as $term ){
$counter++;
?>
<li class="<?php echo !empty( $search_more_less ) && $counter > $search_more_less ? esc_attr( \'term-hidden\' ) : \'\' ?>">
<div class="styled-radio">
<input type="radio" name="<?php echo esc_attr( $name ) ?>" value="<?php echo esc_attr( $term->term_id ) ?>" id="<?php echo esc_attr( $name ) ?>-<?php echo esc_attr( $term->term_id ) ?>" <?php echo $term->term_id == $selected_term ? esc_attr( \'checked="checked"\' ) : \'\' ?>>
<label for="<?php echo esc_attr( $name ) ?>-<?php echo esc_attr( $term->term_id ) ?>"><?php echo $term->name ?></label>
<?php
if( !empty( $term->children ) ){
?>
<a href="javascript:void(0);"><i class="aficon-angle-down"></i></a>
<?php
}
?>
</div>
<?php
if( !empty( $term->children ) ){
?>
<ul class="list-unstyled hidden">
<?php self::taxonomy_listing( $name, $term->children, $taxonomy, $selected_term ); ?>
</ul>
<?php
}
?>
</li>
<?php
}
if( !empty( $search_more_less ) && $counter > $search_more_less ){
?>
<li class="toggle-more-less-wrap">
<a href="javascript:void(0)" data-less="<?php esc_attr_e( \'Show Less\', \'adifier\' ) ?>" data-more="<?php esc_attr_e( \'Show More\', \'adifier\' ) ?>" class="toggle-more-less"><span><?php esc_html_e( \'Show More\', \'adifier\' ) ?></span> <i class="aficon-caret-down"></i></a>
</li>
<?php
}
}
我将非常感谢你的帮助!
SO网友:Christian Mate
好吧,我想我明白了!:D在研究了主题函数分类层次结构是如何完成的之后,我对代码进行了如下修改(不知道它的编码是否正确,但它可以工作):
/*
* Display filter taxonomies
*/
static public function taxonomy_listing( $name, $terms, $taxonomy, $selected_term, $hide_empty = false ){
$search_more_less = adifier_get_option( \'search_more_less\' );
$counter = 0;
foreach( $terms as $term ){
$counter++;
?>
<li class="<?php echo !empty( $search_more_less ) && $counter > $search_more_less ? esc_attr( \'term-hidden\' ) : \'\' ?>">
<div class="styled-radio">
<input type="radio" name="<?php echo esc_attr( $name ) ?>" value="<?php echo esc_attr( $term->term_id ) ?>" id="<?php echo esc_attr( $name ) ?>-<?php echo esc_attr( $term->term_id ) ?>" <?php echo $term->term_id == $selected_term ? esc_attr( \'checked="checked"\' ) : \'\' ?>>
<label for="<?php echo esc_attr( $name ) ?>-<?php echo esc_attr( $term->term_id ) ?>"><?php echo $term->name ?></label>
<?php
if( !empty( $term->children ) ){
?>
<a href="javascript:void(0);"><i class="aficon-angle-down"></i></a>
<?php
}
?>
</div>
<?php
if( !empty( $term->children = adifier_get_taxonomy_hierarchy( $taxonomy, $term->term_id, $hide_empty ) ) ){
?>
<ul class="list-unstyled hidden">
<?php self::taxonomy_listing( $name, $term->children, $taxonomy, $selected_term ); ?>
</ul>
<?php
}
?>
</li>
<?php
}