如何在single.php中仅打印父类别名称? (WordPress的)
我的结构:
A类
-SubCategory X
答案 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>'