0

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.

1
  • 1
    Can you clarify your question? Which specific template files do you want to avoid using? Oct 21, 2011 at 12:22

1 Answer 1

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?

2
  • 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, 2011 at 13:02
  • Can you clarify your question (above) to specify what kind of content you want to include in your Pages? Oct 21, 2011 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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