WordPress 当需要显示文章的父分类和当前分类链接时,我们可以使用这段代码: $categories = get_the_category(); echo '<ul>'; echo '<li> Parent Category: '; foreach( $categories as $cat…
当需要显示文章的父分类和当前分类链接时,我们可以使用这段代码:
$categories = get_the_category();
echo '<ul>';
echo '<li> Parent Category: ';
foreach( $categories as $category ){
if($category->parent != 0){
$parent_category = get_term( $category->parent );
echo '<a href="'%20.%20esc_url(%20get_category_link($parent_category->term_id))%20.%20'">' . esc_html($parent_category->name) . ' </a>';
break;
}
}
echo '</li>';
echo '<li>Subcategory: ';
foreach( $categories as $category ){
if($category->parent != 0){
echo '<a href="'%20.%20esc_url(%20get_category_link($category->term_id))%20.%20'">' . esc_html($category->name) . ' </a>';
}
}
echo '</li></ul>';
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论(0)