WordPress.org

Ready to get started?Download WordPress

Forums

Multiple templates for single archive page (7 posts)

  1. Mark Rowatt Anderson
    Member
    Posted 6 years ago #

    What is the simplest way to have two (or more) different views of the same page?

    For example, I have a category called 'events'. By default, when someone goes to the archive page for an event, it will display with the appropriate template defined by the template hierarchy.

    However, I would also like to be able to display an event with a different template (specifically, a lite version of the page used by an announcements mailing list program, or could also be a lite version for viewing on a mobile device).

    What's the easiest way to achieve this?

    Thanks,

    mark.

  2. moshu
    Member
    Posted 6 years ago #

  3. Mark Rowatt Anderson
    Member
    Posted 6 years ago #

    Thanks Moshu. Unless I am misreading the info and comments for that plugin, that looks like a great way for assigning a template to a category, however if a post has multiple categories it will display the template based one of those categories, but not allow me to select which template to use.

    Or did I misunderstand how that plugin works?

  4. buckyboog
    Member
    Posted 6 years ago #

    Same thing here. A given post in multiple categories will only display using a single template...

  5. buckyboog
    Member
    Posted 6 years ago #

    Progress?

    I can't get the branching single.php to a different single page template beyond the first category specified for a single post.

    But this seems to be able to detect the current category, and return the correct content based on the posts CURRENT category.

    <?php $catname = 0 ;
     $catname = get_query_var('cat');
               if ( $catname =='21') { ?>
    Category 21
    <?php } elseif ( $catname == '19' )  { ?>
    Category 19
    <?php } else  { ?>
    Category Other
    <?php } ?>
  6. buckyboog
    Member
    Posted 6 years ago #

    Does anyone have any improvements/corrections I can make to my PHP? It works... but is there a more correct way to do the code?

  7. Mark Rowatt Anderson
    Member
    Posted 6 years ago #

    This is an ugly hack, but it worked for me. This allows me to select an alternate template (called email) if I add a parameter to the end of a post URL, eg http://www.yoursite.com/blog/post?email=1

    To use, create a file called email-theme.php and put it in the WP plugins folder. The file should contain:-

    <?php
    /*
    Plugin Name: Email Theme
    */
    
    function em_get_template($template) {
    
            if (!empty($_GET["email"])) {
    
                    return "email";
    
            } else {
    
                    return "mytemplate"; //or whatever my primary template is
    
            }
    }
    
    function em_get_stylesheet($stylesheet) {
    
            if (!empty($_GET["email"])) {
    
                    return "mytemplate";
    
            } else {
    
                    return "mandigo"; //or whatever my primary template is
    
            }
    }
    
    add_filter('template', 'em_get_template');
    add_filter('stylesheet', 'em_get_stylesheet');
    
    ?>

    I have no doubt this could be improved upon, for example to allow setting of an arbitary number of templates. I haven't tested at all other than one specific WP installation, but it does the trick for me.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags