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.
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:
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.
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!
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!
Subscribe to RSSStay up to date with my blog
Follow me!Follow me or get followed
iPadCaseDen.comiPad cases, folio cases and bags review