Set multiple templates per post

In Joomla! articles can use multiple templates. I could only put ath the end of url ?template=templatename and article uses the template “templatename”.

Is this possible in WordPress?

Related posts

1 comment

  1. Yes it is. But no by default.

    But it’s pretty easy to do it yourself.

    You can do it in 2 ways:

    1. In single.php template file

    Just add if statement and use get_template_part function to load selected template.

    So your single.php file could look like this:

    <?php
    if ( isset($_GET['template']) ) {
        switch ($_GET['template']) {
            case 'a':
                get_template_part('single-post-template-a');
                break;
            ...        
        }
    } else {
        get_template_part('single-post-template-default');
    }
    

    2. Using single_template hook.

    function get_custom_post_type_template($single_template) {
         global $post;
    
         if ($post->post_type == 'post' && isset($_GET['template']) ) {
              switch ( $_GET['template'] ) {
                   case 'a':
                       return locate_template( array('/single-post-template-a.php') );
                       break;
                   ...
              }
         }
         return $single_template;
    }
    add_filter( 'single_template', 'get_custom_post_type_template' );
    

Comments are closed.