我有多个商品,勾选后会分门别类展示。
现在,我想要实现的是根据点击的类别显示某些图像。
示例。
如果我单击类别 A,则会显示图像 (A)。
我正在按照下面的代码执行此操作。
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<?php
$terms = wp_get_post_terms( get_the_id(), 'type' );
$term_class = '';
if ( isset( $terms[0] ) ) {
$term_class = $terms[0]->slug;
}
?>
<ul>
<li class="linoheight" style="margin-left:45px !important;"><img class="<?php echo $term_class; ?>-new" src="<?php bloginfo('stylesheet_directory'); ?>/images/acoustic-icon-small.png" alt="" width="30px" height="30px" style="display:none;"></li>
<li class="linoheight"><img class="<?php echo $term_class; ?>-new" src="<?php bloginfo('stylesheet_directory'); ?>/images/hard-wired-icon-small.png" alt="" width="30px" height="30px" style="display:none;"></li>
<li class="linoheight"><img class="<?php echo $term_class; ?>-new" src="<?php bloginfo('stylesheet_directory'); ?>/images/radio-icon-small.png" alt="" width="30px" height="30px" style="display:none;"></li>
</ul>
因此,这将获取选中的复选框,然后创建类“categoryA-new”。
在CSS中,这个类会是display:block,显示图标。
现在,我遇到的问题是 <?php echo $term_class;?>-new
仅获取其中一个类别,这意味着如果选择了类别 A,则类别 B 和 C 仍显示图像。
我觉得这是循环的问题,有人有什么想法吗?
最佳答案
您只需要在单击时隐藏和显示该对象,然后该元素的唯一 ID 应传递给某些 jquery 或 javascript 函数,它们将显示该元素,例如, 如果我有
- 第 1 项
- 第 2 项
- 第 3 项
这三个字段都有唯一的id,比如 对于 1- 2- 等你需要像这样在 ul 上放置一些唯一的 id 之后使用 jquery hit 就像首先捕获 li,s 之间的:
$("#list-of-items li").click(function(){
var current_clicked_li_index = $(#list-of-items li).index(this);
// now use .each loop in jquery
$("#list-of-items li img").each(function( Loopindex ) {
if(current_clicked_li_index != Loopindex){
$(this).hide();
}else{
$(this).show();
}
});
});
注意:不要忘记使用 jquery 库。
关于php - 遍历 WordPress 中的类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30173247/