WordPress.org

Ready to get started?Download WordPress

Forums

Page Templates and Post Templates (4 posts)

  1. chuyskywalker
    Member
    Posted 9 years ago #

    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") ) {
    include("$wp_template_dir/single.php");
    exit;

    Change this to:


    /* edited to allow PER POST templates - see also: wp-includes/functions.php */
    } else if ( is_single() && file_exists(get_post_template()) ) {
    include(get_post_template());
    exit;
    /* 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.

  2. Ryan Boren
    WordPress Dev
    Posted 9 years ago #

    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.

  3. chuyskywalker
    Member
    Posted 9 years ago #

    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.)

  4. chuyskywalker
    Member
    Posted 9 years ago #

    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 ;)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.