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.

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"

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

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

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.

share|improve this answer
    
Great thought Jer I am probably trying to create a function that already exists in wordpress. I will read up on this and let you know if it solves my problem. –  Zeusofolus Mar 1 '13 at 19:06
    
Just getting back to let you know it is working great. Thanks so much for opening up my mind to another great feature in WordPress. –  Zeusofolus Mar 1 '13 at 22:00
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.