WordPress函数the_category不提供排除参数。 这样做:
function exclude_post_categories($excl='', $spacer=' ') {
$categories = get_the_category($post->ID);
if (!empty($categories)) {
$exclude = $excl;
$exclude = explode(",", $exclude);
$thecount = count(get_the_category()) - count($exclude);
foreach ($categories as $cat) {
$html = '';
if (!in_array($cat->cat_ID, $exclude)) {
$html .= '<a href="' . get_category_link($cat->cat_ID) . '" ';
$html .= 'title="' . $cat->cat_name . '">' . $cat->cat_name . '</a>';
if ($thecount > 0) {
$html .= $spacer;
}
$thecount--;
echo $html;
}
}
}
}
另外,只要您有此功能,就可以根据需要更改输出。
用法就像:
<?php exclude_post_categories("4"); ?>
它将列出所有类别,但ID为4的类别除外。
翻译自: https://css-tricks.com/snippets/wordpress/the_category-excludes/