$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>query - Exclude categories from the_category();|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

query - Exclude categories from the_category();

matteradmin9PV0评论
function the_category_filter($thelist,$separator=' ') {
    if(!defined('WP_ADMIN')) {
        //list the category names to exclude
        $exclude = array('Something','Something Else','Blah','YAY');
        $cats = explode($separator,$thelist);
        $newlist = array();
        foreach($cats as $cat) {
            $catname = trim(strip_tags($cat));
            if(!in_array($catname,$exclude))
                $newlist[] = $cat;
        }
        return implode($separator,$newlist);
    } else
        return $thelist;
}
add_filter('the_category','the_category_filter',10,2);

I am using that code, to get the_category(); to exclude some categories. But it ain't working, as it is supposed to...

function the_category_filter($thelist,$separator=' ') {
    if(!defined('WP_ADMIN')) {
        //list the category names to exclude
        $exclude = array('Something','Something Else','Blah','YAY');
        $cats = explode($separator,$thelist);
        $newlist = array();
        foreach($cats as $cat) {
            $catname = trim(strip_tags($cat));
            if(!in_array($catname,$exclude))
                $newlist[] = $cat;
        }
        return implode($separator,$newlist);
    } else
        return $thelist;
}
add_filter('the_category','the_category_filter',10,2);

I am using that code, to get the_category(); to exclude some categories. But it ain't working, as it is supposed to...

Share Improve this question asked Oct 15, 2011 at 12:12 BasilakisBasilakis 11 silver badge2 bronze badges 0
Add a comment  | 

4 Answers 4

Reset to default 7

Try using the filter get_the_categories and replace every occurrence of the_category by echo get_the_category() (just to be sure !)

This is what I cooked:

<?php
add_filter('get_the_categories', 'exc_cat');

function exc_cat($cats) {
        //not on admin pages
        if(!is_admin()){
            $exc = array('lipsum', 'dolor');
            foreach ($cats as $i=>$cat){
                if(in_array($cat->name, $exc)){
                   unset($cats[$i]); 
                }
            }
        }
    return $cats;
}
?>

Try my code and let me know if it does your job.

This will work:

Put the following in your functions.php file:

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>1){
                    $html .= $spacer;
                }
            $thecount--;
            echo $html;
            }
          }
      }
}

and then call the function with:

<?php exclude_post_categories('1,5', ', '); ?>

where 1 or 5 are the category IDs you want to exclude.

Code courtesy of http://wordpress/support/topic/the_category-exclude-categories

Enter this handy function from the WordPress support forums — just add it to your theme’s functions.php file:

function the_category_filter($thelist,$separator=' ') {
     // list the IDs of the categories to exclude
     $exclude = array(366);
     // create an empty array
     $exclude2 = array();

     // loop through the excluded IDs and get their actual names
     foreach($exclude as $c) {
          // store the names in the second array
          $exclude2[] = get_cat_name($c);
     }

     // get the list of categories for the current post     
     $cats = explode($separator,$thelist);
     // create another empty array      
     $newlist = array();

     foreach($cats as $cat) {
          // remove the tags from each category
          $catname = trim(strip_tags($cat));

          // check against the excluded categories
          if(!in_array($catname,$exclude2))

          // if not in that list, add to the new array
          $newlist[] = $cat;
     }
     // return the new, shortened list
     return implode($separator,$newlist);
}

// add the filter to 'the_category' tag
add_filter('the_category','the_category_filter', 10, 2);

Initially, I did have some issues with this function; after some tinkering around, I realized I needed a separator declared in the_category() tag, like a comma, dash, etc. In this case, I was using just a space — the_category(' ') — which wasn’t working, but swapping it out for a non-breaking space did the trick: the_category('&nbsp;').

Credit: http://www.stemlegal/greenhouse/2012/excluding-categories-from-the-category-tag-in-wordpres/

I have been looking for a solution for something like this and I make some modifications to some code posted in various topics and I get this..

<?php foreach((get_the_category()) as $cat) {
if (($cat->cat_ID=='#')) 
echo $cat->cat_name;
} ?>

where # = your cat ID. I hope it works for you.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far