Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How do I use if/ else statements instead of custom Page templates? For example, I create content for static Pages that I want to look different from other static Pages. So instead of using page-sample.php, page-sample2.php, etc, could I use conditional statements?

Is there a tutorial that would demonstrate how to do this? It seems that it would save time but I am not familiar enough with using them in WP.

share|improve this question
1  
Can you clarify your question? Which specific template files do you want to avoid using? –  Chip Bennett Oct 21 '11 at 12:22

1 Answer 1

WordPress includes a plethora of conditional tags that can be used in lieu of specific template files.

For example, if you wanted to avoid using date-based archive template files, you could use something like the following in index.php:

<?php
if ( is_archive() ) { // This is a date-based archive
    if ( is_year() ) {
        // This is a year-based archive; 
        // do something
    } else if ( is_month() ) {
        // This is a month-based archive; 
        // do something
    } else if ( is_day() ) {
        // This is a day-based archive; 
        // do something
    } else {
        // I think the above three cases cover everything,
        // but this is a just-in-case, default fallback
    }
}
?>

Do you have specific templates for which you want to use conditionals?

share|improve this answer
    
It's not that I don't want to use specific template files, I want to use conditionals to avoid using custom page templates. For example, I create content for pages that I want to look different than other pages. So instead of using page-sample.php, page-sample2.php, etc, could I use conditional statements? This could also be done for different styled blog pages as well. I'm not sure if I'm making sense - maybe I should just stick with page templates? –  Kevin Oct 21 '11 at 13:02
    
Can you clarify your question (above) to specify what kind of content you want to include in your Pages? –  Chip Bennett Oct 21 '11 at 13:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.