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.

I'm creating my own wordpress theme. I've decided to use a different way to handle the theming and not use the standard get_header() and so functions.

What i do is have a general main.php page that is loaded by every page like blog.php, index.php, page.php and more. This main page, will be then responsible for creating the necessary templates and presenting the related page.

Now, i would ideally want to create sort of like the standard switch that includes files, like :

$page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 'home';

switch($page)
{
    case 'home':           break;
    case 'mail':           break;
    case 'contact':        break;
    default:
        $page = 'home';
}

include("$page.php");

How can i do such a thing for wordpress ? Is there a clean way to know whether an included page is a blog.php, a single.php or other page, so that i can distinguish them?

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

First, Why this is a bad idea

WordPress already provides a rich templating API that allows for other plugins to hook in to your theme where needed. If a plugin needs to provide additional JavaScript or CSS, they tie in to wp_head. If they need to defer some JavaScript to the bottom of the page, they hook into wp_footer. If they add plugins, they use the built-in dynamic sidebar.

By completely bypassing the theme, you're essentially preventing the user from doing anything with their site that you haven't already built in to your display engine. You're also throwing out about half of WordPress from the get-go.

An alternative

Instead, I suggest you work with the existing system. WordPress provides a lot of hooks, template tags, and conditional flags that let you detect which page you're on and load different templates when needed.

There's a really good discussion of the general template hierarchy in the Codex.

You can set up a different page template for each page, then when you write the page in WordPress you just select the right template.

So rather than include("$page.php"), you'd have three files in your theme:

  • page_home.php
  • page_mail.php
  • page_contact.php

Then, at the top of each of these files, you include the following header:

<?php
/**
 * Template Name: [YourPageTemplateName]
 * Describe what the template is for
 *
 * @package [YourThemeName]
 */

For example, from page_contact.php in my "Stacked" theme:

<?php
/**
 * Template Name: Contact
 * This file handles contact pages.
 *
 * @package StackedTheme
 */
share|improve this answer
    
oh i see, this is pretty interesting. I generally feel quite uncomfortable with starting tags in a header.php file and closing them in single.php, for instance. Or repeating myself with the same structure in multiple fies. But i see the point about compatibility for sure, and that is a strong point. –  SpyrosP Jul 5 '11 at 20:15
    
@SpyrosP You can open and close <?php ?> in one file without a problem. You don't have to leave it open. –  kaiser Jul 5 '11 at 20:20
    
@kaiser, i meant the <div> tags actually :) –  SpyrosP Jul 5 '11 at 21:10
2  
Not a problem. Just think of the templating engine as a way to break your work across multiple files. If done right, you can actually avoid breaking <div>s across files ... the only tags you really need to break up are <body> and <html>. –  EAMann Jul 5 '11 at 21:29
add comment

You can simply use the query for such a thing. In reality the different pages don't exist. The get only loaded if the template exists.

It's enough to write:

if ( is_home() || is_front_page() )
{
    // do stuff
}
elseif ( is_archive() )
    // etc.
}
share|improve this answer
add comment

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.