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.

I am developing a common theme for 25 websites, the default requirements are done. Each site has got different single post template. I need to group it as a single theme so i don't need to hard-code every time and maintain a common platform.

The various single posts template are normal-blog / tab structure / List view I am now creating an option page for my theme, so that the user can create a layout on his own for the single-page without hardcoding(options might be drag and drop). I need to create certain components like tab,sidebar, list, and save templates for different categories. the values in tabs could be from a custom field,taxonomy. user can create n no of templates for various categories/tags/taxonomy and save it. For Eg: Template 1 for category sports,education.

I trying to do it but i don't have any clue to it, might be the theme atuhalpa came close. how to solve it.

share|improve this question
add comment

2 Answers

It sounds like you want users to be able alter the structure of the single.php format without much WordPress know-how (perhaps it's for clients?).

You could create an options page for this, and allow the user to select the structure they prefer.

In your functions.php or a plugin, you use a hook to call a function when WordPress loads a page. This function then uses is_single() to check that if it is a single page is required, you would then get WordPress to check to see which of the single templates has been selected and then change the template being called up (which is by default single.php if it exists). Instead it could call up single-normal.php, single-tab.php, etc (or just single.php). You would then create each of these for the various format.

For example:

add_filter('template_include', 'template_switch');
function template_switch( $template ){
    if( is_single()) {
    switch (get_option('single-template')){
            case 'tab':
            $template = locate_template( 'single-tab.php');
            break;

            case 'list':
            $template = locate_template( 'single-list.php');
            break;

            case 'normal':
            $template = locate_template( 'single-normal.php');
            break;
        }  
    }
    return $template;
}

The single-normal.php is perhaps superfluous, you could use single.php. Also you would need to add an options page, and register the 'single-template' option with Wordpress. For details see Codex's Options Reference and creating options pages.

Obviously you would have to create the files 'single-normal.php', 'single-tab.php' etc.

Edit: Or, rather than using the switch statement, you could be really clever and (if an option has been selected) simply call up the template: 'single-'.$option.'php', where $option is the return of the get_option('single-template'). This means you can always add more template-styles by creating the appropriate page, and updating the available options and the above code can remain unchanged :D

Hope this helps!

share|improve this answer
    
If you go this route, you do not want to name your template files single-foobar.php. The Template Hierarchy interprets foobar as a post-type in this context. Instead, I would use something outside the Template Hierarchy, such as content-foobar.php, and use get_template_part( 'content', $option ). –  Chip Bennett Oct 14 '11 at 13:44
    
@chipBennett Thank you for pointing that out. I certainly agree it would be good practise to use a different format to prevent it from potentially clashing with post-type, and the get_template_part be a good idea too. That said I'm not sure if the above scenario would cause problems. WordPress will call up a template for the single page (of any post-type), say single-foobar.php and then the function above overrides it. So a post type foobar would still use single-option.php. –  Stephen Harris Oct 14 '11 at 14:48
    
@Stephen Harris, thanks for the input, my situation is like installing only a single theme with a lot of options, each site has got a lot of components like tab,list,drop down etc. i need to construct a theme as such the user can select the component,structure it(may be a drag and drop) and save the template and mark it for a few categories. With the components in place the user should be able to create as many templates he want. –  Ezhil Oct 15 '11 at 3:42
add comment

Your question is slightly unclear, but if I'm following you correctly: you need one Theme, that has several templates for single.php?

If that's the case, I would recommend creating the base Theme, and then using Child Themes to customize single.php. Doing so is the simplest way to override a single template file several times, and minimizes code repetition.

share|improve this answer
    
sir, thanks for the input, my situation is like installing only a single theme with a lot of options, each site has got a lot of components like tab,list,drop down etc. i need to construct a theme as such the user can select the component,structure it(may be a drag and drop) and save the template and mark it for a few categories. With the components in place the user should be able to create as many templates user wants. –  Ezhil Oct 15 '11 at 3:41
add comment

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.