如果产品<li>
的值为零,我想隐藏类别元素$count
。
这是我正在处理的页面:http://www.hotsales.com.br/procurar/
&#34; Categorias&#34;是类别过滤器。如果类别行的产品为零,则必须隐藏它(可能是style="display: none;"
)。当选择一些商店(Loja)时,许多类别将被列为&#34;计算机(0)和#34;什么时候不应该显示,因为商店可能是鞋子。
我已经尝试将它放在foreach循环结束if ($count = 0) {document.getElementByTagName("li").style.display = "none"}
但是不起作用。这是显示过滤器的实际整体功能。定义$ count后,<li>
元素是生成4行的echo:
/*
List categories on the search sidebar
*/
if( !function_exists( 'list_search_sidebar_cats' ) ){
function list_search_sidebar_cats( $ancestors, $parent, $selected = '', $search_show_count, $permalink ){
global $slugs;
$children = get_terms( 'offer_cat', array( 'parent' => $parent ) );
if( !empty( $children ) ){
echo '<ul class="list-unstyled">';
foreach( $children as $child ){
$li_class = in_array( $child->term_id, $ancestors ) ? 'active' : '';
if( !empty( $selected ) ){
$li_class .= $child->slug == $selected->slug ? ' current' : '';
}
$count = '';
if( $search_show_count == 'yes' ){
$count = custom_term_count( $child, 'offer_cat' );
}
if( empty( $ancestors ) || ( !empty( $ancestors ) && in_array( $child->term_id, $ancestors ) ) || ( !empty( $ancestors ) && !empty( $selected ) && $child->parent == $selected->term_id ) ){
echo '<li style="display:flex;" class="'.esc_attr( $li_class ).'"><a href="javascript:;" data-cat="'.esc_attr( $child->slug ).'">'.$child->name.' <span class="count">('.$count.')</span></a>';
if( !empty( $li_class ) ){
list_search_sidebar_cats( $ancestors, $child->term_id, $selected, $search_show_count, $permalink );
echo '</li>';
}
else{
echo '</li>';
}
}
}
echo '</ul>';
}
} }
答案 0 :(得分:2)
没有必要将javascript放入该剧中以在渲染后更改它 您可以轻松地将其包含在初始输出中:
// please review your conditions here, they don't make too much sense.
// if this is true then this is false (and vice versa)
if( empty( $ancestors ) || ( !empty( $ancestors ) && in_array( $child->term_id, $ancestors ) ) || ( !empty( $ancestors ) && !empty( $selected ) && $child->parent == $selected->term_id ) ){
// here's the fix:
$display = $count>0 ? "flex" : "none";
echo '<li style="display:'.$display.';" class="'.esc_attr( $li_class ).'"><a href="javascript:;" data-cat="'.esc_attr( $child->slug ).'">'.$child->name.' <span class="count">('.$count.')</span></a>';
if( !empty( $li_class ) ){
list_search_sidebar_cats( $ancestors, $child->term_id, $selected, $search_show_count, $permalink );
echo '</li>';
} else{
echo '</li>';
}
}