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

时间:2015-08-01 13:32:56

标签: php wordpress-theming wordpress

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

我的结构:

A类

-SubCategory X

  • 帖子(文章)仅发布到子类别X.
  • 我只想打印"类别A"的名称。(不带html标签)。

2 个答案:

答案 0 :(得分:0)

我想你试试这段代码: 对我来说,这是有效的............

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

答案 1 :(得分:0)

<?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;
    }

&GT;

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