the_category排除某个分类

WordPress的the_category()未提供排除某个分类的传参功能,如果要使用这个功能,可以新建个函数来实现,在WordPress主题 functions.php中插入:

function exclude_post_categories($excl='', $spacer=' ') {
  $categories = get_the_category(get_the_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的分类。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注