dosgy00204 2017-04-20 12:29
浏览 62
已采纳

wordpress get_category_by_slug if true add add class

Good afternoon , this is the problem : I have an asside menu , this menu has 4 categories and each of this categories has many subcategories , What I am trying to do is that when I am on the page of a subcategory I give a class to a

on the left menu , a class of active so the category of the subcategory I am in stays opens . Whole code here : https://pastebin.com/Q09eE9nB
$categories = get_categories( array(
'orderby' => 'name',
'order'   => 'ASC',
'parent' => 52,
'hide_empty' => 0

));

            <?php if( $categories ): ?>
        <ul class="main-categ">
            <?php foreach ( $categories as $kCat => $vCat ): ?>
                <li class="main-item <?php echo get_category_by_slug( $categories ) == $subCategories ? 'active' : ''; ?>"><?php
                                $subCategories = get_categories( array(
                                    'orderby' => 'name',
                                    'order'   => 'ASC',
                                    'parent' => $vCat->cat_ID,
                                    'hide_empty' => 0
                                ));?>

                <a class="main-link" href="#<?php echo $vCat->slug; ?>" data-cat="<?php echo $vCat->slug; ?>"><?php echo $vCat->name; ?></a>
                <?php if( $subCategories ): ?>
                <ul class="subcateg">
                    <?php foreach ( $subCategories as $kSub => $vSub ): ?>
                    <li><a href="<?php echo esc_url( get_category_link( $vSub->cat_ID ) ); ?>"><i class="fa fa-angle-double-right"></i> <?php echo $vSub->name; ?></a></li>
                    <?php endforeach; ?>
                </ul>
                <?php endif; ?>
            </li>
            <?php endforeach; ?>
        </ul>
  • 写回答

2条回答 默认 最新

  • douwen9345 2017-04-20 13:10
    关注

    Solution :

    $thisCat = get_category( get_query_var( 'cat' ) );
    
    
                <ul class="main-categ">
                <?php foreach ( $categories as $kCat => $vCat ): ?>
                    <li class="main-item <?php echo $thisCat->category_parent == $vCat->cat_ID ? 'active' : ''; ?>"><?php
                                    $subCategories = get_categories( array(
                                        'orderby' => 'name',
                                        'order'   => 'ASC',
                                        'parent' => $vCat->cat_ID,
                                        'hide_empty' => 0
                                    ));?>
    
                    <a class="main-link" href="#<?php echo $vCat->slug; ?>" data-cat="<?php echo $vCat->slug; ?>"><?php echo $vCat->name; ?></a>
                    <?php if( $subCategories ): ?>
                    <ul class="subcateg">
                        <?php foreach ( $subCategories as $kSub => $vSub ): ?>
                        <li class="<?php echo get_query_var( 'cat' ) == $vSub->cat_ID ? 'activ':'';?>"><a href="<?php echo esc_url( get_category_link( $vSub->cat_ID ) ); ?>"><i class="fa fa-angle-double-right"></i> <?php echo $vSub->name; ?></a></li>
                        <?php endforeach; ?>
                    </ul>
                    <?php endif; ?>
                </li>
                <?php endforeach; ?>
            </ul>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目
  • ¥15 数电几道习题,写出作答过程,ai一律不采用
  • ¥15 利用pthon计算薄膜结构的光导纳
  • ¥15 海康hlss视频流怎么播放
  • ¥15 Paddleocr:out of memory error on GPU
  • ¥30 51单片机C语言数码管驱动单片机为AT89C52