如果类别包含子类别,如何在类别页面上显示子类别,否则显示该类别的帖子WordPress类别页面
答案 0 :(得分:1)
您可以尝试使用带有args child_of函数的get_categories(),如下所示。
$args = array('child_of' => 'category_id');
$categories = get_categories( $args );
foreach($categories as $category) {
echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
echo '<p> Description:'. $category->description . '</p>';
echo '<p> Post Count: '. $category->count . '</p>';
}
此外,$category->count ===0
则类别没有帖子。我们可以使用它来检查该类别的帖子。
答案 1 :(得分:1)
$id = $wp_query->get_queried_object_id(); // now you have the ID of the category
现在检查是否返回了某些东西,然后从那里做任何事情:
$args = array('child_of' => $id);
$subcats = get_terms( $args );
if(!empty($subcats)){
foreach($subcats as $subcat) {
echo get_term_link( $subcat->slug, $subcat->taxonomy ); // for example
}
} else {
// do the usual stuff
}