Support » Fixing WordPress » Page Templates and Post Templates

  • I was working on my 1.5 site and thought that being able to define separate templates for Pages was pretty sweet. Makes working with themes really nice. (ie, get_page_template();)

    Then I notied it could be done for category listing by using a file called category-CATNAME.php. Cool.

    However, there was a distinct lack of this kind of feature for individual posts. SOOO, I think that this should feature should be in there. It’s a pretty quick hack, and for the most part covered.

    Summary: Allows individual posts to use a specialy defined template.

    Open wp-blog-header.php line 234:

    } else if ( is_single() && file_exists("$wp_template_dir/single.php") ) {

    Change this to:

    /* edited to allow PER POST templates - see also: wp-includes/functions.php */
    } else if ( is_single() && file_exists(get_post_template()) ) {
    /* End addition */

    Now, open up wp-includes/functions.php goto 1466 and add a new function:

    function get_post_template() {
    global $wp_query;
    $id = $wp_query->post->ID;
    $template_dir = get_template_directory();
    $default = "$template_dir/single.php";
    $template = get_post_meta($id, "wp_post_template", true);
    if (empty($template)) {
    return $default;
    if (file_exists("$template_dir/$template")) {
    return "$template_dir/$template";
    return $default;

    (You’ll notice get_page_template() is at line 1448 )

    Now to use this, I didn’t make a fancy drop down or anything like that for the post page like there is on the Page, er, page. However, you can just set the meta key “wp_post_template” with a value of something like “special.php” and if there is a special.php it will use that, if not it will default to single.php, or index.php in case of crazy failure.

Viewing 3 replies - 1 through 3 (of 3 total)
  • We won’t do per post templates by default, but we just added filters for all of the different template types. You can create a plugin that hooks your function to the ‘single_template’ filter and makes WP select post templates according to your whims.

    Ah, that would be easier – any pointers to where I can find some more of that code?

    (A plugin would be a much better option.)

    Hey, I just got the 02-01 build and made the plugin. Thanks a bunch for setting up those filters! Having the functionality in plugin format makes updating to each nightly a much saner process 😉

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Page Templates and Post Templates’ is closed to new replies.