Categories
Web Development WordPress

Save Coding Time by Creating Special-Case Categories in WordPress

When you would use excluded categories:

When using WordPress as more of a content management system (CMS) than a blogging platform, there are many things that you need control over. One of them is special-case categories.

  • Frequently asked questions
  • Testimonials
  • Case studies
  • Press releases

When you have a category of posts that you don’t want to have comments, publishing dates, post author, etc., you can define a list of excluded categories. In most cases, you should use Category Templates to achieve this functionality, but that is not always practical or the best option.

If you use the ACE plugin

I use the Advanced Category Excluder Plugin for nearly every WordPress installation I work with. That being said, there is always custom functionality that needs to find out what categories are being excluded by the plugin. Instead of hard-coding the Excluded categories, you can use this function (in your theme’s functions.php file, or Thesis Theme’s custom_functions.php file) to find out what categories are being excluded:

global $kws_exclude;
$kws_exclude = excluded_categories();
function excluded_categories($type='array') {
	global $wpdb, $wp_query, $ace_targets;
	if (is_array($ace_targets) && get_option("ace_settings_hide"))
	{
		foreach ($ace_targets as $key=>$val)
		{
			if (!empty($wp_query->$key) && $wp_query->$key == 1) $filter = $key;
		}
		$cats_to_exclude = get_option("ace_categories_".$filter);
	}
	if(strtolower($type) == 'array') { $cats_to_exclude = explode(',', $cats_to_exclude); }
	return $cats_to_exclude;
}

If you want to hard-code excluded categories

If you don’t use the Advanced Category Excluder plugin, or simply want to use this functionality in a different way, here’s how to hard-code (manually define) the categories to exclude. Add this code to your functions.php file.

	global $exclude;
	$exclude = array();
	$excludes = get_categories('child_of=10'); // Exclude sub-categories of a specific category (in this case, 10)
	$excludes2 = get_categories('8'); // Exclude a specific category (in this case, 8)
	$excludeList = array_merge($excludes, $excludes2); // Combine the two excludes
	foreach($excludeList as $e) { $exclude[] = $e->cat_ID; } // Build the master list of excludes

Great, now what?

Then, when you want to find out if the post or category is excluded in another function, you do this:

function any_function_you_are_working_with() {
	global $exclude; // make sure to add this
	if(is_category($exclude) || in_category($exclude)) {
		// If the post or category is, or is in an excluded category, do this
	} else {
		// If not, do this
	}
}

This way, you can customize how content appears based on whether it’s an excluded category (removing comment forms, date/time, etc…).

2 replies on “Save Coding Time by Creating Special-Case Categories in WordPress”

Have you run into any search problems when excluding content this way?

I have a theme, that excludes a portfolio category from showing up in the blog… but it also excludes the portfolio category from the Search results in the site.

No, no search problems. I used this method on Egret Windows website, and it’s indexed nicely.

This post is somewhat out of date though; I think there are more elegant solutions now with custom post types, etc. It’ll still work though!

Comments are closed.