我使用了这段代码:
$categories = wp_get_post_categories(get_the_ID());
foreach($categories as $category){
echo '<div class="col-md-4"><a href="' . get_category_link($category) . '">' . get_cat_name($category) . '</a></div>';
}
但只返回一个类别,如何获取所有类别?
在您提供给我们的代码中,您选择了为特定帖子选择的类别 get_the_ID() 正在执行该部分。但是你最好使用另一个函数 get_categories()https://developer.wordpress.org/reference/functions/get_categories/ https://developer.wordpress.org/reference/functions/get_categories/你会喜欢这样做:
$categories = get_categories();
foreach($categories as $category) {
echo '<div class="col-md-4"><a href="' . get_category_link($category->term_id) . '">' . $category->name . '</a></div>';
}
您还可以传递更具体的参数(如果需要) - 请参阅https://developer.wordpress.org/reference/functions/get_terms/ https://developer.wordpress.org/reference/functions/get_terms/有关您可以通过的详细信息
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)