GKB Solutions
  • About Us
  • Our Services
  • Clients
  • Contact Us

Warning: Use of undefined constant header_mobile_top - assumed 'header_mobile_top' (this will throw an Error in a future version of PHP) in /home/customer/www/gkbsolutions.com/public_html/wp-content/themes/gkb-custom-responsive-gkb/includes/header-mobile.php on line 3

Warning: Use of undefined constant options_logo - assumed 'options_logo' (this will throw an Error in a future version of PHP) in /home/customer/www/gkbsolutions.com/public_html/wp-content/themes/gkb-custom-responsive-gkb/includes/header-mobile.php on line 11
GKB Solutions

Warning: Use of undefined constant header_mobile_bottom - assumed 'header_mobile_bottom' (this will throw an Error in a future version of PHP) in /home/customer/www/gkbsolutions.com/public_html/wp-content/themes/gkb-custom-responsive-gkb/includes/header-mobile.php on line 27
You are here: Home / Marketing Tips / Wordpress Tip: Exclude categories from "the_category()" template tag

March 30, 2010 by Greg Bengston

WordPress Tip: Exclude categories from "the_category()" template tag

Here is another helpful wordpress tip involving the “categories” template tag “the_category()”. By default, the category template tag shows which category your post is in by creating an unordered, vertical list with bullet points. Most people then control the look of the category list via css to make it better fit into the design of their theme.

Many of us will have the same post associated with multiple categories and want all of them to be displayed. The “the_category()” template tag will output all of those associated categories. But, some of us also use categories for structure… to display a post in an appropriate place in a particular theme design. For example, when you want a particular post featured you might use a category labeled “Featured” to ensure that the post shows up in a certain place on your page.

Here is the problem… unless you want the category list to display “Featured” next to it’s more appropriate category classifications you need to make a change to the category template tag’s output.

If only wordpress would allow a simple “exclude category” parameter in the template tag, but it currently does not. But, with a little extra coding you can accomplish this.

Take a look at the code below:
[php]<?php
//edit below for categories you want to be excluded
$exclude = array("Featured", "Uncategorized");
//edit below for how you want the category items separated
$separator = " | ";
//don’t edit below here!!
$new_the_category = ”;
foreach((get_the_category()) as $category) {
if (!in_array($category->cat_name, $exclude)) {
$new_the_category .= ‘<a href="’.get_bloginfo(url).’/’.get_option(‘category_base’).’/’.$category->slug.’">’.$category->name.’</a>’.$separator;
}
}
echo substr($new_the_category, 0, strrpos($new_the_category, $separator));
?>[/php]

Simply replace the “the_category()” with the above code and list the category names you want to be invisible where we have “Featured”, “Uncategorized”.

NOTE: Depending on how you have your permalinks set up, you will probably have to explicitly state “category” in the permalinks settings for the urls to work properly.

Filed Under: Marketing Tips Tagged With: cms, wordpress

Website:

GKB Solutions, LLC
205 Swinburne Rd
West Chester, PA 19382

CONTACT US

FIND US ON:

  • Sitemap
  • Privacy Policy
Copyright © 2006 - 2023  · All Rights Reserved.

  • Home
  • About Us
  • Our Services
  • Our Clients
  • Contact Us
  • Home
  • About Us
  • Our Services
  • Our Clients
  • Contact Us