简体   繁体   中英

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

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

My structure:

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM