Dual themes and how to use cat_is_ancestor_of()


What kind of WordPress themes are called dual? Have you ever seen Blog-Portfolio mixed theme? Some developers take the advantage of parent-child Page relation in order to build such theme but others achieve dual functionality by creating 2 separate, root level categories. In both cases content requires different templates for a case designer wants to ensure different visual experience for the site user.

Not sure about other developers but I prefer the second method of having categorized Portfolio. This method dictates certain rules, for example using 4 extra templates. Why four? Normally you would have 2 standard WordPress templates: category.php and single.php. In our case we will use category.php and single.php as redirect pages either to Blog or Portfolio category page or Blog and Portfolio single page. Otherwise spoken we will now have category-blog.php, category-portfolio.php, single-blog.php and single-portfolio.php templates extra.

Redirection

WordPress knows exactly who belongs where. Imagine you want to read the details of a certain Post whose title is clickable. After you click on it, WordPress takes you to the template named single.php and renders the content. This is how things would work ‘normally’. In our story, when taken to single.php, the logic must be extended a little bit:

  • Find out which category our Post belongs to
  • Check who’s an ancestor of this category
  • Redirect user to either single-blog.php or single-portfolio.php

In order to achieve all that, consider the following code:


<?php
  // this is how our 'single.php' tempate looks like
  // template switch between Blog and Portfolio - single post
  $blog_ID = 1; // ID of our blog root-level category
  $portfolio_ID = 2; // ID of our portfolio root-level category
  // our post belongs to this category...
  $categ_object = get_the_category();
  // who's our ancestor, blog or portfolio?
  if( cat_is_ancestor_of( $blog_ID, (int)$categ_object[ 0 ]->cat_ID ) ) include( TEMPLATEPATH . "/single-blog.php" );
  else if( cat_is_ancestor_of( $portfolio_ID, (int)$categ_object[ 0 ]->cat_ID ) ) include( TEMPLATEPATH . "/single-portfolio.php" );
  ?>

Category ID of a Blog or Portfolio can either be hardcoded or pulled dynamically. So how it works? get_the_category() function will return required Post parent category ID. Hence we already know IDs of our root-level categories, the only thing we need now is to check who’s the ancestor. cat_is_ancestor_of() function will do the task for us and according to result (boolean, true or false) user gets redirected to the right template.

Category level redirection

Almost everything mentioned before will work at category level redirection. Because of the fact that we have 2 top-level categories as well, and we want to make them render differently, redirection by means of cat_is_ancestor_of() is required. So here we go:


<?php
  // this is how our 'category.php' tempate looks like
  // template switch between Blog and Portfolio - category
  $blog_ID = 1; // ID of our blog root-level category
  $portfolio_ID = 2; // ID of our portfolio root-level category
  // we are here now
  $categ_object = get_category( get_query_var( 'cat' ), false );
  // who's our ancestor, blog or portfolio? First check for root category.
  if( (int)$categ_object->category_parent > 0 ) {
    if( cat_is_ancestor_of( $blog_ID, (int)$categ_object->cat_ID ) ) include( TEMPLATEPATH . "/category-blog.php" );
    else if( cat_is_ancestor_of( $portfolio_ID, (int)$categ_object->cat_ID ) ) include( TEMPLATEPATH . "/category-portfolio.php" );
  } else {
  if( (int)$categ_object->cat_ID == $blog_ID ) include( TEMPLATEPATH . "/category-blog.php" );
  else if( (int)$categ_object->cat_ID == $portfolio_ID ) include( TEMPLATEPATH . "/category-portfolio.php" );
  }
?>

Any comments, questions, ideas and suggestions are welcomed!

Share with others...

deliciousdiggreddittechnoratifacebooktwittergoogleyahoowikioblinklistsimpyspurl
Comments (4)

Track comments via RSS 2.0 feed. Feel free to post the comment, or trackback from your web site.

  • MandyMoore on Jun 16, 2009

    Thanks for sharing this information mate, it’s always good to read original articles.

  • Nixx on Jun 18, 2009

    It doesn’t work for me. All I did was plain copy-paste of your code but something went wrong. There’s no PHP error, var_dump doesn’t output any value.

  • Feeleep on Jun 19, 2009

    @Nixx
    Did you use (int) in the front of $categ_object->cat_ID? That’s important part, otherwise it won’t work.

  • Nixx on Jun 21, 2009

    Yeap, it works now. For some reason I thought (int) can be removed since PHP automatically makes the difference between string and string-number. Thanks!

Leave a Comment

Search

Latest comment by Nixx

Yeap, it works now. For some reason I thought (int) can be removed since PHP automatically makes the difference between string and string-number. Thanks!

Stay in touch!

Subscribe Subscribe to RSSStay up to date with my blog

Twitter Follow me!Follow me or get followed

iPadCaseDen.com iPadCaseDen.comiPad cases, folio cases and bags review

Advertisement

GraphicRiver ThemeForest