多年来,我一直在我的一个网站上使用这段代码:
<?php
if (is_category()) {
$this_category = get_category($cat);
if (get_category_children($this_category->cat_ID) != "") {
echo "<ul class=\'subsubcats\'>";
wp_list_categories(\'orderby=title&show_count=0&title_li=
&use_desc_for_title=1&child_of=\'.$this_category->cat_ID);
echo "</ul>";
} else {
get_template_part(\'loop\'); // if no sub categories exist, show the posts
}
}
?>
这将在类别概述页面中显示所有子类别。如果没有子类别,它将显示包含的帖子。我注意到,在WP Debug中,自WP 2.8以来,这段代码实际上已被弃用,因此现在是替换它的时候了。
我已经读到,我应该用get\\u术语来代替它。我找到了一段几乎可以满足我需要的代码:
<?php
$terms = get_terms([
\'taxonomy\' => get_queried_object()->taxonomy,
\'parent\' => get_queried_object_id(),
]);
echo \'<ul class="subsubcats"><li class="categories"><ul>\';
foreach ( $terms as $term) {
echo \'<li class="cat-item"><a href="\' . get_term_link( $term ) . \'">\' . $term->name . \'</a></li>\';
}
echo "</ul></li></ul>";
get_template_part(\'loop\'); // if no sub categories exist, show the posts
?>
但它有一个错误:它显示所有类别和子类别概述中的帖子,而旧代码仅在没有子类别可用时显示帖子。我不知道如何调整此代码以使其按我需要的方式工作,非常感谢您的帮助!