Monday, March 15, 2010

Multiple layouts for each Wordpress Page

The solution:
Edit ‘page.php’ to check what page your on, and then load the appropriate template.

The implementation:
If you edit page.php you’ll notice that it calls the get_header() and get_footer() functions. This is about the only bit of code that we want to keep in this file.

First Step:
First, make a copy of page.php and give it a name. This is your first template. Delete the header and footer calls from your template, because we’re going to load this into page.php, and it will have already called them.

Second Step:
Open page.php and delete everything between the header and footer calls. In this space, add a PHP conditional that uses the is_page() function.

if(is_page(107) == true) {
include ‘page_full.php’;
} else {
include ‘page_twocolumn.php’;
}

?>

Here’s an example of what I did to load multiple page layouts:

if(is_page(107) == true) {
include ‘page_films.php’;
} else if(is_page(array(518, 28, 13)) == true) {
include ‘page_full.php’;
} else {
include ‘page_twocolumn.php’;
}

?>

This sets page with ID 107 to use the template specific for the ‘films’ page. It also sets pages with ID’s 518, 28, and 13 to use the full page layout. All others use the two column layout.

That’s it!

0 comments: