【发布时间】:2015-08-01 13:32:56
【问题描述】:
如何在 single.php 中只打印父类别名称? (wordpress)
我的结构:
A类
-子类别 X
- 帖子(文章)仅发布到子类别 X。
- 我只想打印“A 类”的名称。(不带 html 标记)。
【问题讨论】:
标签: php wordpress-theming wordpress
如何在 single.php 中只打印父类别名称? (wordpress)
我的结构:
A类
-子类别 X
【问题讨论】:
标签: php wordpress-theming wordpress
我想你试试这个代码: 对我来说这是工作............
<?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 echo get_category_parents( get_query_var('cat') , true , '' ); ?> 但我得到:可捕获的致命错误:WP_Error 类的对象无法转换为字符串
<?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>'
【讨论】: