Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果post有父添加类_Php_Wordpress - Fatal编程技术网

Php 如果post有父添加类

Php 如果post有父添加类,php,wordpress,Php,Wordpress,所以我有一个无论做什么都无法解决的问题,提前感谢您的帮助: 我想做的是,当我在post页面上时,我给父类一个类,给它的父类一个类 我使用以下代码获取帖子id及其类别id: get_header(); $theCategory = get_the_category(); $categories = get_categories( array( 'orderby' => 'name', 'order' => 'ASC', 'parent' => 52,

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

我想做的是,当我在post页面上时,我给父类一个类,给它的父类一个类

我使用以下代码获取帖子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) ?>
get_header();
$theCategory=get_the_category();
$categories=get_categories(数组(
'orderby'=>'name',
“订单”=>“ASC”,
“父项”=>52,
“hide_empty”=>0
));
$thiscat=get_queryed_object_id();
$catobject=get_category($thiscat,false);//根据当前类别的id获取类别对象
$catobject2=获取类别($catobject,false);
$parentcat=$catobject2->category_parent;//父类别的id
?>
在此之后,如果帖子属于正确的类别,我将尝试在此处添加一个活动类:

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


    对不起,问题不清楚。@JenithSamuel,我有一个菜单,这个菜单有4个类别,每个类别有许多子类别,每个子类别有许多产品。。。当我在产品页面上时,我想向第一个子类别添加一个类别。该类别将为活动子类别添加不同的颜色