Multiple templates for single custom post type

Does anybody know of a way to modify the wordpress template process so that a single custom post type can be displayed on two separate templates based on the value of a custom field.

For example I have a single Custom Post Type called “plans”

Read More

It is using the single-plans.php and archive-plans.php files as templates by default.

What I would like to do is use this bit of code to filter out some of the results within my loop.

<?php //Filter the results based on the presence of $var
   $var = get_post_meta($post->ID, 'my_custom_field', true);
    if($var !== '') { ?>
      THE LOOP
    <?php } ?>

This works fine but i need create two separate templates where I can test for separate $var
so I can have two menu items that each call on a separate version of the template and get a unique set of results from my single CPT

Thanks, Eric

Related posts

Leave a Reply

1 comment

  1. Might be side-stepping your question, but can you use get_template_part?

    <?php 
    
        get_template_part( 'plans', get_post_meta($post->ID, 'my_custom_field', true ) ); 
    ?>
    

    Then create “plans_plana.php” and “plans_planb.php” (assuming that ‘my_custom_field’ can have values of ‘plana’ and ‘planb’).

    twentyten and twentyeleven make heavy use of get_template_part if you need some more examples.