<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Anthony L Rivera &#187; is_subcategory()</title>
	<atom:link href="http://anthonylrivera.net/tag/is_subcategory/feed/" rel="self" type="application/rss+xml" />
	<link>http://anthonylrivera.net</link>
	<description>Front-End Developer</description>
	<lastBuildDate>Wed, 12 Oct 2011 22:57:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>is_subcategory()</title>
		<link>http://anthonylrivera.net/228/2009/12/is_subcategory/</link>
		<comments>http://anthonylrivera.net/228/2009/12/is_subcategory/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 20:48:32 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[category]]></category>
		<category><![CDATA[is_subcategory()]]></category>

		<guid isPermaLink="false">http://anthonylrivera.net/?p=228</guid>
		<description><![CDATA[Ok, I just spent the better part of ALL DAY trying to figure this out. I want my wordpress category.php template to be able to detect whether the category it is serving is a parent category or subcategory. THATS IT. And without having to hard code any category ID&#8217;s. After perusing the forums and codex [...]]]></description>
			<content:encoded><![CDATA[<p>Ok, I just spent the better part of ALL DAY trying to figure this out.</p>
<p>I want my wordpress category.php template to be able to detect whether the category it is serving is a parent category or subcategory. THATS IT. And without having to hard code any category ID&#8217;s. After perusing the forums and codex for a couple of hours, the marriage of these two functions seemed to get the job done.</p>
<p><a href="http://codex.wordpress.org/Function_Reference/get_query_var" target="_blank">get_query_var</a> is the variable stored in the query that tells category.php what category to serve.</p>
<p><a href="http://http://codex.wordpress.org/Function_Reference/get_category" target="_blank">get_category</a> retrieves category data given a category ID or category object.</p>
<p>Here&#8217;s the function that goes in your functions.php file:</p>
<pre>function is_subcategory (){
    $cat = get_query_var('cat');
    $category = get_category($cat);
    return ( $category-&gt;parent == '0' ) ? true : false;
}</pre>
<p>Then you can use this in your category.php file to determine if the category you are serving is a parent category or subcategory:</p>
<pre>
if ( is_subcategory() ):
     //displaying subcategory
else:
    //displaying parent category
endif;
</pre>
<p>Hope this helps some people out there.</p>
<p><em>*note: both code snippets should be wrapped inside php brackets. &lt;?php  ?&gt;   </em></p>
]]></content:encoded>
			<wfw:commentRss>http://anthonylrivera.net/228/2009/12/is_subcategory/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

