the_category mit Ausschlüssen

Avatar of Chris Coyier
Chris Coyier am

Die WordPress-Funktion the_category bietet keinen Ausschluss-Parameter. Das tut sie

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;
      }
    }
  }
}

Außerdem können Sie, solange Sie das haben, die Ausgabe nach Belieben ändern, was schön ist.

Die Anwendung ist wie folgt:

<?php exclude_post_categories("4"); ?>

Dies würde alle Kategorien auflisten, mit Ausnahme derjenigen mit der ID 4.