繁体   English   中英

遍历WordPress中的类别

[英]Looping through categories in WordPress

我有很多产品,打勾后会显示在不同的类别中。

现在,我想要实现的是根据要单击的类别显示某些图像。

例。

如果我单击类别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仍在显示图像。

我感觉这是循环的问题,有人有任何想法吗?

您只需要隐藏并显示对象(如果onclick),则应将该项目的唯一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库。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM