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.

I have a problem and I don't know how to solve it. I tried some solutions but I failed since I am not good programer. I also tried to find a solution, but there is no similar question asked. So... here is the problem.

I have a custom post type called "Arrangements", and one of the custom fields associated with that CPT is select box called "Nights" (rtm_nights), with options:

  • no nights
  • 1 night
  • 2 nights
  • 3 nights

In my single-arrangements.php there is a get_template_part function that call content-arrangements.php, and now I made 4 similar files, because I need to reorganize arrangements template:

  • content-oneday.php
  • content-twoday.php
  • content-threeday.php
  • content-fourday.php

Now... what do I need is to get some of these content templates depending of what is chosen in rmt_nights field.

  • If "no nights" is chosen content-oneday.php should be called via get_tempalte_part.
  • If "1 night" is chosen content-twoday.php should be called via get_tempalte_part.
  • If "2 nights" is chosen content-threeday.php should be called via get_tempalte_part.
  • If "3 nights" is chosen content-fourday.php should be called via get_tempalte_part.

I tried something like this in different versions but it is obviously a wrong approach:

<?php if ( get_post_meta($post->ID, 'rtm_nights', '3 nights') ) ; ?>
<?php get_template_part( 'content', 'fourday' ); ?>
<?php if ( get_post_meta($post->ID, 'rtm_nights', '2 nights') ) ; ?>
<?php get_template_part( 'content', 'threeday' );  ?>
<?php if ( get_post_meta($post->ID, 'rtm_nights', '1 night') ) ; ?>
<?php get_template_part( 'content', 'twoday' );  ?>
<?php if ( get_post_meta($post->ID, 'rtm_nights', 'no nights') ) ; ?>
<?php get_template_part( 'content', 'oneday' );  ?>
<?php endif; ?>
share|improve this question

2 Answers 2

up vote 1 down vote accepted

It's a PHP question, and not related to WordPress, but I'm answering because you are messing with codes. Just make it easy, and you also can go through it. :)

<?php
/**
*  get your custom field data and store into a variable
*  to make it easy - nothing else
*/
$my_custom_field = get_post_meta( $post->ID, 'rtm_nights', $single=true );

if ( $my_custom_field == '3 nights' ) {
    get_template_part( 'content', 'fourday' );
} else if ( $my_custom_field == '2 nights' ) {
    get_template_part( 'content', 'threeday' );
} else if ( $my_custom_field == '1 night' ) {
    get_template_part( 'content', 'twoday' );
} else if ( $my_custom_field == 'no nights') {
    get_template_part( 'content', 'oneday' );
} //endif
?>

EDIT

And you are missing something about get_post_meta() syntax:

<?php get_post_meta( $post_id, $key, $single ); ?>

Value cannot be checked through it. :)

share|improve this answer
    
Thank you Mayeenul. This is working exactly as I wanted. –  pendjer Nov 25 '13 at 11:28
    
Pleasure that helped you. :) –  Mayeenul Islam Nov 25 '13 at 11:30

It is a syntax question, not really a wordpress one. However:

According to the get_post_meta() Function Reference, the third argument is used to

return a single result, as a string

if it is true and not to make a comparison.

You have to get the custom field and check its value.

if ( get_post_meta($post->ID, 'rtm_nights') == '3 nights' ) {
  get_template_part( 'content', 'fourday' );
} else if ( get_post_meta($post->ID, 'rtm_nights') == '2 nights' ) {
  get_template_part( 'content', 'threeday' );
} else if ( get_post_meta($post->ID, 'rtm_nights') == '1 nights' ) {
  get_template_part( 'content', 'twoday' );
} else if ( get_post_meta($post->ID, 'rtm_nights') == 'no nights' ) {
  get_template_part( 'content', 'oneday' );
} 
share|improve this answer
    
Thanks Lucio. I aslo test your solution but it breaks my template. However, thank you for the support. –  pendjer Nov 25 '13 at 11:29

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.