简体   繁体   English

如何在single.php中仅显示父类别名称? (WordPress的)

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

How to print only Parent Category name in single.php? 如何在single.php中仅打印父类别名称? (wordpress) (WordPress的)

My structure: 我的结构:

Category A A类

-SubCategory X -子类别X

  • The post (article) is posted only to Subcategory X. 帖子(文章)仅发布到子类别X。
  • I want to print only the name of "Category A".(without html tag). 我只想打印“类别A”的名称。(不带html标记)。

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>'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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