【问题标题】:Wordpress - How To Get Parent Category IDWordpress - 如何获取父类别 ID
【发布时间】:2013-11-13 18:04:55
【问题描述】:

Wordpress - 如何获取父类别 ID


my category is  
news
---->sport news

我在体育新闻中有一篇文章。

当我进入体育新闻帖子时如何获得父母(新闻)ID?

此代码回显父猫名称

      foreach((get_the_category()) as $childcat) { $parentcat = $childcat->category_parent;  
echo get_cat_name($parentcat);
echo $parentcat->term_id;}   
        echo $post->post_parent->cat_ID; 

此代码回显单页猫名

   global $post;$category = get_the_category($post->ID);echo $category[0]->name;

猫名的这个 chode 回显 id

        $category = get_the_category();    echo $category[0]->cat_ID; 

我需要回显父 ID (cat_ID) 请帮帮我

谢谢。

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    简单,非常简单。

    //firstly, load data for your child category
    $child = get_category(31);
    
    //from your child category, grab parent ID
    $parent = $child->parent;
    
    //load object for parent category
    $parent_name = get_category($parent);
    
    //grab a category name
    $parent_name = $parent_name->name;
    

    学习get_category

    【讨论】:

      【解决方案2】:
      $thiscat =  get_query_var('cat'); // The id of the current category
      $catobject = get_category($thiscat,false); // Get the Category object by the id of current category
      $parentcat = $catobject->category_parent; // the id of the parent category 
      

      【讨论】:

      • 非常感谢,
      【解决方案3】:
      <?php 
          if (is_category()) 
          {
          $thiscat = get_category( get_query_var( 'cat' ) );
          $catid = $thiscat->cat_ID;
          $parent = $thiscat->category_parent;
          if (!empty ($catid) ) {
          $catlist = get_categories(
                                      array(
                                      'child_of' => $catid,
                                      'orderby' => 'id',
                                      'order' => 'ASC',
                                      'exclude' => $parent,
                                      'hide_empty' => '0'
                                      ) );
      
              ?>
                  <div class="subcat-list">
                      <ul>
                          <?php foreach ($catlist as $category) { ?>
                            <li><a href="<?php echo get_category_link( $category->term_id ); ?>"><?php echo $category->name; ?></a></li>
                            <?php } ?> 
                       </ul>
                  </div>
                  <?php } } ?>
      

      【讨论】:

      • 请在您的回答中添加解释,说明它是如何解决问题的。
      【解决方案4】:

      对于 Wordpress 5.9。

      获取父类别ID:

      $cat = get_queried_object();
      $parentCatId = $cat->parent;
      

      获取当前类别ID:

      $cat = get_queried_object();
      $catId = $cat->term_id
      

      通过 id 获取类别名称:

      $name = get_the_category_by_ID($catId);
      

      【讨论】:

        猜你喜欢
        • 2014-04-22
        • 1970-01-01
        • 2020-12-06
        • 1970-01-01
        • 2023-01-18
        • 2017-09-24
        • 2020-03-05
        • 1970-01-01
        • 2012-09-29
        相关资源
        最近更新 更多