doumao6212 2015-05-11 16:46
浏览 59

循环使用WordPress中的类别

I have a number of products, which will be displayed in different categories when ticked.

Now, what I am trying to achieve, is certain images to be shown depending on which categories are clicked.

Example.

IF I clicked category A, then an image (A) would be shown.

I am doing this as the code shows below.

<?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>

So, this will get the checkbox which is selected, and then create the class 'categoryA-new.

In the CSS, this category will be display:block, which displays the icon.

Now, the issue I am having, is that <?php echo $term_class;?>-new is only getting one of the categories, which means if category A is selected, then category B and C are still showing images.

I have a feeling this is an issue with the loop, does anyone have any ideas?

  • 写回答

1条回答 默认 最新

  • duanjia7607 2015-08-02 20:48
    关注

    You just need to hide and show the object if onclick then a unique id of that item should be passed to some jquery or javascript function and they will show the item eg, if I have

    • item 1
    • item 2
    • item 3

    these three fields have unique id,s in them like for 1- 2- etc you need to use jquery hit like this after placing some unique id at the ul like first to catch the in between 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();
    
       }
    
    });   
    
    });
    

    Note: don't forget to use the jquery library.

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP