$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'); ?>categories - Exclude a category from the_category in the single post page|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)

categories - Exclude a category from the_category in the single post page

matteradmin11PV0评论

I want to include something like the the_category function in a WordPress post, which by default echoes a list of links to all categories the post is attached separated by comma, but I want to exclude some categories from listing in certain views. In some cases I have to exclude all children ones from appearing, in others a top category or random specific categories.

I want to include something like the the_category function in a WordPress post, which by default echoes a list of links to all categories the post is attached separated by comma, but I want to exclude some categories from listing in certain views. In some cases I have to exclude all children ones from appearing, in others a top category or random specific categories.

Share Improve this question edited Jan 26, 2011 at 18:30 peroyomas asked Jan 26, 2011 at 15:23 peroyomasperoyomas 3394 silver badges13 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

Not sure but i think you can try this

function my_category_filter($cats){
   //exclude these from displaying
   $exclude = array("Large Announcement", "Announcement", "News");
   $Catds = array();
   foreach ($cats as$cat){
      if (!in_array($cat,$exclude)){$Catsa[] = $cat;}
   }
   return $Catsa;
}

add_filter('the_category','my_category_filter');

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(' ').

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

Post a comment

comment list (0)

  1. No comments so far