Wordpress - How To Get Parent Category ID

后端 未结 3 803
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-08 04:11

Wordpress - How To Get Parent Category ID


my category is  
news
---->sport news

i have a post in

相关标签:
3条回答
  • 2021-02-08 04:41

    Simply, very simply.

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

    Study get_category

    0 讨论(0)
  • <?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 } } ?>
    
    0 讨论(0)
  • 2021-02-08 04:44
    $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 
    
    0 讨论(0)
提交回复
热议问题