<?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>Jared Atchison<title>&#187; wordpress sub categories</title>
</title>
	<atom:link href="http://www.jaredatchison.com/tag/wordpress-sub-categories/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jaredatchison.com</link>
	<description>Websites. Wordpress. Genesis.</description>
	<lastBuildDate>Tue, 07 Sep 2010 03:09:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>WordPress parent and sub category IDs</title>
		<link>http://www.jaredatchison.com/2009/10/27/wordpress-parent-and-sub-category-ids/</link>
		<comments>http://www.jaredatchison.com/2009/10/27/wordpress-parent-and-sub-category-ids/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 22:40:46 +0000</pubDate>
		<dc:creator>j-atchison</dc:creator>
				<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[wordpress categories]]></category>
		<category><![CDATA[wordpress sub categories]]></category>

		<guid isPermaLink="false">http://jaredatchison.com/?p=89</guid>
		<description><![CDATA[The current project I am working left me needing to access the category ID (parent) and sub category ID for the sidebar.php I have several sections in the sidebar for posts using is_single() that get the most popular posts, recent posts, etc. The problem using the code below &#8211; which works if you don&#8217;t deal [...]]]></description>
			<content:encoded><![CDATA[<p>The current project I am working left me needing to access the category ID (parent) and sub category ID for the sidebar.php</p>
<p>I have several sections in the sidebar for posts using is_single() that get the most popular posts, recent posts, etc. The problem using the code below &#8211; which works if you don&#8217;t deal with sub categories &#8211; is it only gets the parent category ID.</p>
<pre class="brush: php;">
 $categories = get_the_category();
 $this_cat_ID = $categories[0]-&gt;cat_ID;
 $this_cat_name = $categories[0]-&gt;cat_name;
 $this_cat_url = get_category_link($this_cat_ID);
</pre>
<p>For example:<br />
I have <em>News</em> category that has an ID of 6, a <em>Press Release</em> sub category with the ID of 7, and a <em>TV Coverage </em>sub category with the ID of 8.<br />
The sidebar.php contains a block that displays the most recent posts.<br />
If I am viewing a press release post (single.php) I want to see the the <strong>only</strong> latest Press Release posts. Using $this_cat_ID, which is the parent category ID, it is going to show all the posts in the <em>News</em> including not only <em>Press Release </em>but also <em>TV Coverage</em>.</p>
<p>It&#8217;s easy to get the parent category id as I showed above, and while it isn&#8217;t difficult to get the sub category ID (or IDs if you have more than one), most examples I found on the WP.org forums didn&#8217;t do what I needed. You need to add a few lines to get the sub cat.</p>
<pre class="brush: php;">
$categories = get_the_category();
 $this_cat_ID = $categories[0]-&gt;cat_ID;
 $this_cat_name = $categories[0]-&gt;cat_name;
 $this_cat_url = get_category_link($this_cat_ID);
 // get the sub category if we have them
 foreach ($categories as $cat) {
    $parent = $cat-&gt;category_parent;
    if ($parent != 0 ){
       $sub_cat_ID = $cat-&gt;cat_ID;
       $sub_cat_name = $cat-&gt;cat_name;
       $sub_cat_url = get_category_link($sub_cat_ID);
    }
 }
</pre>
<p>With this code you still have access to the parent category ID but also gain the sub category ID.</p>
<p>All that is left is to just write a quick function that checks to see if there is a sub category, something that contains:</p>
<pre class="brush: php;">
if (!$sub_cat_ID) {
   echo $this_cat_ID;
} else {
   echo $sub_cat_ID;
}
</pre>
<p>Problem solved! Now viewing a post filed under <em>News</em> will show the most recent News posts, a post filed under <em>Press Coverage</em> will show the most recent Press Coverage posts, and so on.</p>
<p>FYI &#8211; this method only works if you are going to have a post in one sub category at a time, otherwise you will have to modify things so the sub category info gets stuck in an array.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jaredatchison.com/2009/10/27/wordpress-parent-and-sub-category-ids/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
