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.

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

share|improve this question
    
can you explain more what you mean by "except some minor changes"? how are these custom single post templates different? –  birgire Jun 7 '13 at 11:12
    
@birgire Check the Update within my question :) –  Sven Jun 7 '13 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 Jun 7 '13 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 Jun 7 '13 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 Jun 7 '13 at 11:52

2 Answers 2

up vote 2 down vote accepted

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

<?php
global $post;
get_template_part('singlepost',$post->ID);
?>

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

Important Link:

get_template_part()

share|improve this answer
    
Thx, but is there any difference to my add_filter? –  Sven Jun 7 '13 at 12:09
1  
by using this you don't have to update your functions.php.read this post –  Ravs Jun 7 '13 at 13:16

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

<?php
  /*
  Template Name: Page, two columns
  */
  get_header();
?>

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.

share|improve this answer
    
Good point, but I think that's only for pages,... –  Sven Jun 9 '13 at 9:59
    
Indeed, requires a plugin to support it: wordpress.org/plugins/custom-post-template –  syslogic Jun 9 '13 at 23:48
    
you could switch by post-type (instead of IDs): codex.wordpress.org/Post_Type_Templates –  syslogic Jun 9 '13 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). –  syslogic Jun 9 '13 at 23:55

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.