title: How to display all subcategories of deepest category on post (WordPress)?
body: <p>I would like to display (sidebar) all the subcategories (linked) for the second-deepest level category a post belongs to.  I have tried various code (like the snippet below that is definitely flawed (it produces duplicates of subcategories in the display list). I think the easiest way might be to follow breadcrumbs (I have a breadcrumbs widget on the same page. So basically just display all subcategories of the deepest level category. (Also, sometimes various categories are checked (in more than one strain of categories eg South Sudan post might be in the strain below, but also checked for the Human Rights category. I don't really care which category strain I display, just so long as it displays one of them).</p>
  
  <p>For example, </p>
  
  <p>On a post about South Sudan news, which is breadcrumbed: World > Africa > East Africa > South Sudan, it would display all the subs of the East Africa category</p>
  
  <p>On a post about Vancouver Island (breadcrumbed World > North America > Canada > BC > Vancouver Island), it would display all sub-categories to BC).</p>
  
  <p>Is there even a way to do this? </p>
  
  <p>The (flawed) code that I've been working with (not sure if there might be a better starting point).</p>
  
  <pre><code>&lt;?php
  $post_categories = wp_get_post_categories( $post-&gt;ID );
  foreach($post_categories as $c){
      $cat = get_category( $c );
  
      $childCats = get_categories( array('child_of' =&gt; $c) );
      if(is_array($childCats)):
          foreach($childCats as $child){ ?&gt;
              &lt;h2&gt;&lt;?php echo $child-&gt;name; ?&gt;&lt;/h2&gt;
              &lt;?php
              query_posts('cat='.$child-&gt;term_id);
              while(have_posts()):
                  the_post();
                  $do_not_duplicate = $post-&gt;ID;
                  //post code stuff here;
              endwhile;
              wp_reset_query();
          }
      endif;
  }
  ?&gt;
  </code></pre>