如果post有父类添加类

时间:2017-04-20 16:36:40

标签: php wordpress

所以我有这个问题,无论我做什么都无法解决,提前感谢您的帮助:

我想要做的是,当我在帖子页面上时,我将一个班级给父母一个班级

我使用以下代码获取帖子ID及其类别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) ?>

在此之后,如果帖子在正确的分类中,我试图在这里添加一个活跃的类:

<?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 个答案:

没有答案