The Issue: I am looking for custom single post templates, to add or remove individual elements as a function of a normal single post.

There are a lot of ways to create custom post templates for single posts within WordPress. Especially post formats are a great chance to use default templates for default cases; however I need real custom templates for every post.

The Idea: My first approach was to add an if/else statement according to the post ID:

// check if custom post
if ( is_single('999') )
    // check if there is a custom post template file
    if ( file_exists(TEMPLATEPATH . '/single-999.php' )
        // use custom post template
        return TEMPLATEPATH . '/single-999.php'; 
// use normal post template for everything else
include(TEMPLATEPATH . '/single.php');

Well, this isn't wrong, but it would totally mess up my template code if there will be more and more special cases. So maybe I can use a filter to always use a custom template if there is one that corresponds with the post ID:

add_filter( 'single_template', function( $template ) {
    // check if there is a custom post template file
    if ( file_exists(TEMPLATEPATH . '/single-' . $GLOBALS['post']->ID . '.php') )
        // use custom post template
        return TEMPLATEPATH . '/single-' . $GLOBALS['post']->ID . '.php';
    // use normal post template for everything else
    return $template;

But now I will end up with a lot of custom template files, that are all the same except some minor changes.

More Thoughts: I think I would rather like to add dynamic sections within the single.php template, to hook in with custom includes/filters?

The Question: Is there a different (more exclusive) approach to get those kind of custom post templates?

Update: In a lot of these special cases I need to add some additional StyleSheets or JavaScripts, but also custom container with HTML and also PHP content (this is why I tried to do it with custom templates instead of custom fields). Most of the time the additional elements are above, below or beside the_content().

  • can you explain more what you mean by "except some minor changes"? how are these custom single post templates different?
    – birgire
    Commented Jun 7, 2013 at 11:12
  • @birgire Check the Update within my question :)
    – Sven
    Commented Jun 7, 2013 at 11:34
  • some themes use metaboxes in the post edit page for this kind of customization, so you don't have to edit the logic inside functions.php or add a new single-123.php file each time you add a new post.
    – birgire
    Commented Jun 7, 2013 at 11:42
  • @birgire Is it a good idea to insert PHP content this way? I don't have to update functions.php; but I need to create one more single-{id}.php file...
    – Sven
    Commented Jun 7, 2013 at 11:50
  • some themes allow PHP in metaboxes, I don't like it. You could try to use shortcodes or use forms with options/selects/inputs.
    – birgire
    Commented Jun 7, 2013 at 11:52

3 Answers 3


I think this approach will work.

1.create template for single post like singlepost.php( default single post template),singlepost-99.php,singlepost-101.php.

2.now put just this code in single.php

global $post;

what this code does check for single post template for current post by post id if not found call singlepost.php.

Important Link:


  • 1
    by using this you don't have to update your functions.php.read this post Commented Jun 7, 2013 at 13:16

This is finally possible with WordPress 4.4 (as announced on Make WordPress Core).

The WordPress template hierarchy now allows single custom post templates with a naming pattern like this:


Quoting from John Blackbourn the templates follows these rules:

This template follows the rules of is_single() and is used for a single post or custom post type. [...] It comes in the hierarchy before single.php and single-{post_type}.php.


In order to make it a template, you have to add a comment:

  Template Name: Page, two columns

Whatever name you assign in the comment -

can be selected as template within WP-admin sidebar.

Like this you won't end up with hundreds of different template-bits.

I mean, even if the above solution works - one has to consider maintenance.

  • Good point, but I think that's only for pages,...
    – Sven
    Commented Jun 9, 2013 at 9:59
  • Indeed, requires a plugin to support it: wordpress.org/plugins/custom-post-template Commented Jun 9, 2013 at 23:48
  • you could switch by post-type (instead of IDs): codex.wordpress.org/Post_Type_Templates Commented Jun 9, 2013 at 23:52
  • b/c the above solution requires the user to manually maintain files and ID - instead of simply using an already prepared template (which can get problematic, because every revision and auto-saved edit will get a new ID assigned). Commented Jun 9, 2013 at 23:55

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.