0

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; ?>

2 Answers 2

1

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. :)

2
  • Thank you Mayeenul. This is working exactly as I wanted.
    – pendjer
    Commented Nov 25, 2013 at 11:28
  • Pleasure that helped you. :) Commented Nov 25, 2013 at 11:30
1

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' );
} 
1
  • Thanks Lucio. I aslo test your solution but it breaks my template. However, thank you for the support.
    – pendjer
    Commented Nov 25, 2013 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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