<?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>Sean&#039;s Blog &#187; get_categories</title>
	<atom:link href="http://seanbehan.com/tag/get_categories/feed/" rel="self" type="application/rss+xml" />
	<link>http://seanbehan.com</link>
	<description>Web Programming, Ruby on Rails, Wordpress, PHP from Burlington, Vermont</description>
	<lastBuildDate>Wed, 18 Jan 2012 21:44:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Get Child Categories of Parent Category on Single Post in WordPress</title>
		<link>http://seanbehan.com/programming/get-child-categories-of-parent-category-on-single-post-in-wordpress/</link>
		<comments>http://seanbehan.com/programming/get-child-categories-of-parent-category-on-single-post-in-wordpress/#comments</comments>
		<pubDate>Thu, 17 Jun 2010 15:51:26 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[children]]></category>
		<category><![CDATA[functionality]]></category>
		<category><![CDATA[get_categories]]></category>
		<category><![CDATA[parent]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=1056</guid>
		<description><![CDATA[I was hoping to find a function in the WordPress API, that goes something like this&#8230; the_child_categories(&#34;Vermont&#34;); // print Burlington, Brattleboro... etc if this post has any child categories of Vermont But I could not. The result that I did find, from various forums goes something like this&#8230; $parentcat = get_category_by_slug('Vermont'); foreach((get_the_category()) as $childcat): if [...]]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:px;">
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fseanbehan.com%2Fprogramming%2Fget-child-categories-of-parent-category-on-single-post-in-wordpress%2F&amp;layout=button_count&amp;show_faces=false&amp;width=&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:27px;" allowTransparency="true"></iframe>
				</div><div class="really_simple_share_twitter" style="width:px;">
					<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Get Child Categories of Parent Category on Single Post in WordPress" data-url="http://seanbehan.com/programming/get-child-categories-of-parent-category-on-single-post-in-wordpress/" 
						data-via="" ></a> 
				</div></div>
		<div style="clear:both;"></div><p>I was hoping to find a function in the WordPress API, that goes something like this&#8230;</p>
<pre class="wp-code-highlight prettyprint">
  the_child_categories(&quot;Vermont&quot;);
  // print Burlington, Brattleboro... etc if this post has any child categories of Vermont
</pre>
<p>But I could not. The result that I did find, from various forums goes something like this&#8230;</p>
<pre class="wp-code-highlight prettyprint">
$parentcat = get_category_by_slug('Vermont');
foreach((get_the_category()) as $childcat):
  if (cat_is_ancestor_of($parentcat, $childcat)):
    echo get_category_link($childcat-&gt;cat_ID);
    echo $childcat-&gt;cat_name;
  endif;
endforeach;
</pre>
<p>If you&#8217;re in the in the Loop, you can use the get_the_category() function to retrieve the category objects of the current post, without printing the results to the screen. You can loop over the contents from what is returned by the function.</p>
<p>This condition uses the API function &#8220;cat_is_ancestor_of&#8221; to check whether or not the category from the current post is a child of the parent which we fetched with the &#8220;get_category_by_slug&#8221; function.</p>
<p>Here is a little function that I threw into &#8220;functions.php&#8221; so that I can get the child categories easily in my theme.</p>
<pre class="wp-code-highlight prettyprint">
/** Usage
_the_category_children(&quot;Vermont&quot;); */
function _the_category_children($slug=&quot;&quot;){
  if($categories       = get_the_category()):
    if($slug_category   = get_category_by_slug($slug)):
      foreach($categories as $category):
        echo (cat_is_ancestor_of($slug_category, $category)) ? $category-&gt;cat_name : '';
      endforeach;
    endif;
  endif;
}
</pre>
<p>More info/references on accomplishing this functionality are available<br />
<a href="http://codex.wordpress.org/Function_Reference/cat_is_ancestor_of">http://codex.wordpress.org/Function_Reference/cat_is_ancestor_of</a><br />
<a href="http://wordpress.org/support/topic/284057?replies=8#post-1120489">http://wordpress.org/support/topic/284057?replies=8#post-1120489</a><br />
<a href="http://wordpress.org/support/topic/297615">http://wordpress.org/support/topic/297615</a></p>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/programming/get-child-categories-of-parent-category-on-single-post-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

