0

I was going through a tutorial on Justin Tadlock's blog but it uses the single_template filter, which was removed in 3.4. The codex says to use {$type}_template but if I use "post_template" with the code on that page it doesn't work.

2
  • post format is not good for you?
    – chifliiiii
    Commented Aug 18, 2012 at 23:29
  • I don't think so. I'm wanting to add microformat data to existing review articles and it would be nice to clean up the single.php file. Ideally, this post category would be set up as a custom post type but I don't know if I want to go through converting them. I can do everything I need in the single.php template but was thinking the example in the linked blog article would be nice for organization.
    – Bjorn
    Commented Aug 19, 2012 at 0:24

1 Answer 1

3

The answers you want are in the code. Meaning, you need to go exploring.

Your search should start with wp-includes/template-loader.php. This is the very last file WordPress includes before the theme get's loaded. it runs through a bunch of conditionals to check which type of page we're out and what template to fetch.

The relevant (offending?) line:

<?php
elseif ( is_single() && $template = get_single_template() ) :

Now that we know get_single_template fetches templates for single posts, we can go find that function in wp-includes/template.php.

<?php
/**
 * Retrieve path of single template in current or parent template.
 *
 * @since 1.5.0
 *
 * @return string
 */
function get_single_template() {
    $object = get_queried_object();

    $templates = array();

    $templates[] = "single-{$object->post_type}.php";
    $templates[] = "single.php";

    return get_query_template( 'single', $templates );
}

No filters yet, but you're starting to see how WP locates templates. Now we need to see what get_query_template does (get_query_template is also in wp-includes/template.php).

<?php
/**
 * Retrieve path to a template
 *
 * Used to quickly retrieve the path of a template without including the file
 * extension. It will also check the parent theme, if the file exists, with
 * the use of {@link locate_template()}. Allows for more generic template location
 * without the use of the other get_*_template() functions.
 *
 * @since 1.5.0
 *
 * @param string $type Filename without extension.
 * @param array $templates An optional list of template candidates
 * @return string Full path to file.
 */
function get_query_template( $type, $templates = array() ) {
    $type = preg_replace( '|[^a-z0-9-]+|', '', $type );

    if ( empty( $templates ) )
        $templates = array("{$type}.php");

    return apply_filters( "{$type}_template", locate_template( $templates ) );
}

The first argument is the type of template we're goign to be locating (single for this question) and the second is a series of templates to search. This is the point where we stop. locate_template just searches the child and parent theme directories for each of the files in the $templates array -- it returns the file path if one is found or an empty string if it isn't.

Notice that $type in this function is "single".

I wrote all the above, so I could tell you that the filter is still single_template. The actual string single_template is not found in the source, but the filter still exists (and it works).

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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