One of the great features of wordpress is the conditional tags. Those are very powerful tags that you can use in templates to change content depending on a condition.
There’s especially one very handful tag, when it comes to creating custom templates: is_page_template() . With this tag, you can check which template is being used by the current page. The use is quite simple:
[php classname="myclass" collapse="false" firstline="1" gutter="true" light="false" padlinenumbers="false" smarttabs="true" tabsize="4" language="false"]
is_page_template(‘MyCustomTemplateName.php’)[/php]
(don’t forget the .php extension or it won’t work.)
This tag becomes very handy, when for example you want to display a different sidebar for your custom template, so your sibar.php should look something like:
[php classname="myclass" collapse="false" firstline="1" gutter="true" light="false" padlinenumbers="false" smarttabs="true" tabsize="4" language="false"]<?php
if ( is_page_template(‘MyCustomTemplateName.php’) ) {
// put your sidebarcode for the custom template here
} else {
// put your sidebarcode for the rest of your site here
}
?>
[/php]
Seams pretty simple hu ? Unfortunately, if you use lots of queries in different parts of your template, this may mess around with the conditional tag, and the is_page_template() may not work properly anymore. I encountered this issue while making a template to display posts from a specific category. I called the sidebar AFTER the posts query, and the is_page_template() would not work. Funny part, it worked if I called the sidebar before the post queries.
Fortunately, there’s a little “trick” to make this work: before your conditional tag, you should add wp_reset_query();. By doing this, we bring the query back to its original state (when the page was called). This trick is hidden in wordpress codex here as a trick to make the footer work, but it works as well on sidebar issues.
Your code should now look something like
[php classname="myclass" collapse="false" firstline="1" gutter="true" light="false" padlinenumbers="false" smarttabs="true" tabsize="4" language="false"]<?php
wp_reset_query();
if ( is_page_template(‘MyCustomTemplateName.php’) ) {
// put your sidebarcode for the custom template here
} else {
// put your sidebarcode for the rest of your site here
}
?>
[/php]
Hope this will help some people out there, I wish I had found this before, I would have saved 4hours of search on google
// disclaimer: this trick was tested on WP3.0.1 version using the default twenty-ten theme, you may need to adapt the advice to your wp version/theme