the_category with Excludes in WordPress

Some Related Posts:-

The WordPress function the_category doesn’t offer an exclude parameter. This does:

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

Plus as long as you have that, you can alter the output however you want which is nice.

See also  Create CSS Only Modal Window Using Details Element

Usage is like:

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

Which would list all categories excluding the one with the ID of 4.

Leave a Reply

Your email address will not be published. Required fields are marked *