How to show only Parent Category name in single.php? (wordpress)

How to print only Parent Category name in single.php? (wordpress)

My structure:

Read More

Category A

-SubCategory X

  • The post (article) is posted only to Subcategory X.
  • I want to print only the name of “Category A”.(without html tag).

Related posts

2 comments

  1. I think you try this code:
    for me this is working…………

    <?php $parentscategory ="";
    foreach((get_the_category()) as $category) {
    if ($category->category_parent == 0) {
    $parentscategory .= ' <a href="' . get_category_link($category->cat_ID) . '" title="' . $category->name . '">' . $category->name . '</a>, ';
    }
    }
    echo substr($parentscategory,0,-2); ?>
    
  2. <?php 
        $get_parent_cats = array(
            'parent' => '0' //get top level categories only
        ); 
        $all_cats = get_categories( $get_parent_cats );
    
        foreach( $all_categories as $single_category ){
    
           $catID = $single_category->cat_ID;
        }
    

    ?>

           echo '<li><a href=" ' . get_category_link( $catID ) . ' ">' . $single_category->name . '</a>'
    

Comments are closed.