2 pieces of similar PHP, one works one doesn't

2019-08-04 17:42发布

I've got a few fields on a property site, grabbing a value in English & depending on the value, translating it (if another language other than English is selected).

This piece of code works fine:

<?php if(get_post_meta($post->ID,'prop_parking',true) && $prop_parking):
    $prop_parking_meta = get_post_meta($post->ID,'prop_parking',true);
    if ($prop_parking_meta == 'Yes') {
        $prop_parking_meta = '<!--:en-->Yes<!--:--><!--:es-->Sí<!--:--><!--:ru-->да<!--:-->';
    }
    elseif ($prop_parking_meta == 'No') {
        $prop_parking_meta = '<!--:en-->No<!--:--><!--:es-->No<!--:--><!--:ru-->нет<!--:-->';
    } ?> 
         <li>
           <p><?php echo PROP_PARK_CSTM;?>:</p><p> <?php _e( $prop_parking_meta ); ?></p>
         </li>
<?php endif; ?>

I get back Yesin the set language, yet in this field I don't (I just see Yes or No):

<?php if(get_post_meta($post->ID,'prop_garage',true) && $prop_garage):
    $prop_garage_meta = get_post_meta($post->ID,'prop_garage',true);
    if ($prop_garage_meta == 'Yes') {
        $prop_garage_meta = '<!--:en-->Yes<!--:--><!--:es-->Sí<!--:--><!--:ru-->да<!--:-->';
    }
    elseif ($prop_garage_meta == 'No') {
        $prop_garage_meta = '<!--:en-->No<!--:--><!--:es-->No<!--:--><!--:ru-->нет<!--:-->';
    } ?>        
          <li>
           <p><?php echo PROP_GARG_CSTM;?>:</p><p> <?php _e( $prop_garage_meta ); ?></p>
          </li>
<?php endif; ?>

Is it something obvious I'm missing? :( Thanks!

1条回答
贪生不怕死
2楼-- · 2019-08-04 18:13

I don't know why this issue happens sometimes in qTranslate, but there are two options to deal with it:

  1. using the shortcode notation

    $prop_garage_meta = '[:en]Yes[:es]Sí[:ru]да';
    
  2. applying the_content filter

    $prop_garage_meta = apply_filters( 
        'the_content', 
        '<!--:en-->Yes<!--:--><!--:es-->Sí<!--:--><!--:ru-->да<!--:-->' 
    );
    
查看更多
登录 后发表回答