1

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

0

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.

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.