dtnpzghys01643322 2017-04-20 16:36
浏览 11

如果post有父类添加类

so I have this Issue that I cannot solve no matter what I do , thanks in advance for help :

What I am trying to do is that when I am on the post page , I give a class to the parent to its parent

I am using the following code to get the post id and its category id:

get_header();
$theCategory = get_the_category();

$categories = get_categories( array(
    'orderby' => 'name',
    'order'   => 'ASC',
    'parent' => 52,
    'hide_empty' => 0
));
$thiscat = get_queried_object_id();
$catobject = get_category($thiscat,false); // Get the Category object by the id of current category
$catobject2 = get_category($catobject,false);
$parentcat = $catobject2->category_parent; // the id of the parent category
?>
    <div class="container">
         <?php echo category_description( get_query_var( 'cat' ) ); ?>
    </div>
  <?php var_dump($thiscat) ?>

After this , if the post is in the right categoty I am trying to add a class of active here :

<?php if( $categories ): ?>
<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"><?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>
<?php endif; ?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 netty整合springboot之后自动重连失效
    • ¥20 wireshark抓不到vlan
    • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
    • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
    • ¥15 stata安慰剂检验作图但是真实值不出现在图上
    • ¥15 c程序不知道为什么得不到结果
    • ¥40 复杂的限制性的商函数处理
    • ¥15 程序不包含适用于入口点的静态Main方法
    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击