<?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>mummey.org &#187; template</title>
	<atom:link href="http://www.mummey.org/category/wordpress/template/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mummey.org</link>
	<description>Personal Website of Adrian Mummey</description>
	<lastBuildDate>Tue, 27 Oct 2009 03:11:46 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Finding a Category Ancestor / Descendant for WP Templates</title>
		<link>http://www.mummey.org/2009/03/finding-a-category-ancestor-descendant-wp-templates/</link>
		<comments>http://www.mummey.org/2009/03/finding-a-category-ancestor-descendant-wp-templates/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 06:01:02 +0000</pubDate>
		<dc:creator>Adrian Mummey</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[ancestor]]></category>
		<category><![CDATA[category]]></category>
		<category><![CDATA[children]]></category>
		<category><![CDATA[descendent]]></category>

		<guid isPermaLink="false">http://www.mummey.org/?p=365</guid>
		<description><![CDATA[Recently I had a problem where I needed to find out if a specific category was a descendant of another category, or if one category was a ancestor of another. The latest website I am working on has a top level category called &#8220;Trips we do&#8221; and below that are many sub-categories and sub-sub categories [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I had a problem where I needed to find out if a specific category was a descendant of another category, or if one category was a ancestor of another. The latest website I am working on has a top level category called &#8220;Trips we do&#8221; and below that are many sub-categories and sub-sub categories etc. I wanted to use the same template on all of the categories that were descended from the &#8220;Trips we do&#8221;.<br />
Wordpress already has a function that returns the parent categories <a href="http://codex.wordpress.org/Template_Tags/get_category_parents">get_category_parents</a> but this returns a string more suited for breadcrumbs. I needed a yes or no answer that my category in question was in fact descended from some other arbitrary category. Here is the function I produced:</p>
<pre><code class="php">function is_ancestor_cat($ancestor, $descendant){
  $ancestor = (string) $ancestor;
  $desc_id = (string) $descendant;
  $child_cats = get_term_children( (string) $ancestor, &#39;category&#39;);
  if($ancestor == $descendant){
    return true;
  }
  else if(!count($child_cats)){
    return false;
  }
  else{
    $is_child = false;
    foreach($child_cats as $cat_id){
      if($cat_id == $desc_id){
        $is_child = true;
        break;
      }
      else{
        $is_child = $is_child || is_ancestor_cat($cat_id, $descendant);
      }
    }
    return $is_child;
  }
}</code></pre>
<p>What this code does is that is grabs all the children for the <em>ancestor</em> category then checks them against the <em>descendant</em> category, it will keep recursing until it hits the leaf nodes and returns a boolean value.<br />
Here it is in action in my template file <em>category.php</em>:</p>
<pre><code class="php">//Check if we are in a category
if(is_category()){
  $ancestor_cat_id = 43;
  //Lets get the current category we are in
  $cur_cat_id = get_cat_id( single_cat_title(&quot;&quot;,false) );
  $is_ancestor = false;
  //Check if we are in the ancestor category
  $is_ancestor = is_ancestor_cat($ancestor_cat_id, $cur_cat_id);

  //If it is an ancestor, use a specific template
  if($is_ancestor){
   include(TEMPLATEPATH.&#39;/category-43.php&#39;);
  }
  //Use the default template
  else{
   include(TEMPLATEPATH.&#39;/category-default.php&#39;);
  }

}</code></pre>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.mummey.org%2F2009%2F03%2Ffinding-a-category-ancestor-descendant-wp-templates%2F&amp;linkname=Finding%20a%20Category%20Ancestor%20%2F%20Descendant%20for%20WP%20Templates"><img src="http://www.mummey.org/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.mummey.org/2009/03/finding-a-category-ancestor-descendant-wp-templates/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
