Wordpress - replace variable with a name

Discussion in 'PHP' started by SharingEngines, Mar 15, 2014.

  1. #1
    Hi,

    I have tis code

    <'.if (get_post_meta($post->ID,"_as_roomtype",true) == 'DE').'>
    <a href="'.get_permalink().'" class="readnow" >"---NAME---"</a>
    Code (markup):
    I have values on "meta_value" like "DE", "UK", "SP"

    I need to make something like that.

    if the value on ""meta_value" = "DE" show Germany on "---NAME---"
    if the value on ""meta_value" = "UK" show United Kingdom on "---NAME---"
    if the value on ""meta_value" = "SP" show Spain on "---NAME---"

    How can I make this?

    Thank you for your help
     
    SharingEngines, Mar 15, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    If you only have a few of these, and feel okay doing this manually, you can just assign the values to an array, something like this:
    
    $countries = array(0 => array(1 => 'DE', 2=> 'Germany'), 1 => array(1 => 'UK', 2 => 'United Kingdom'), 2 => array(1 => 'SP', 2 => 'Spain'));
    
    PHP:
    and then do something like this:
    
    for ($i = 0; $i < count($countries); $i++) {
    echo '<'.if (get_post meta($post->ID,"_as_roomtype",true) == $countries[$i][1]).'>
    <a href="'.get_pemalink().'" class="readnow">'.$countries[$i][2].'</a>';
    }
    
    PHP:
    I think that should work, haven't tested it though
     
    PoPSiCLe, Mar 15, 2014 IP
  3. HassanKhalid

    HassanKhalid Active Member

    Messages:
    158
    Likes Received:
    6
    Best Answers:
    4
    Trophy Points:
    90
    #3
    Try something like this:
    <?php
    $meta_values = array('DE' => 'Germany', 'UK' => 'United Kingdom', 'SP' => 'Spain');
    if ($meta_key = get_post_meta($post->ID,"_as_roomtype",true)):
    ?>
    <a href="<?php echo get_permalink() ?>" class="readnow" ><?php echo $meta_values[$meta_key]; ?></a>
    Code (markup):
     
    HassanKhalid, Mar 15, 2014 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    First of, where the hell is $meta_key assigned? And second, the $meta_key is not compared, it's assigned, and will always return whatever the get_post_meta is returning....
     
    PoPSiCLe, Mar 15, 2014 IP
  5. SharingEngines

    SharingEngines Member

    Messages:
    83
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #5
    Hi,

    Thank you very much for your help.
    Already try many combinations.

    Last combination is

    $meta_values = array('DE' => 'Germany', 'UK' => 'United Kingdom', 'SP' => 'Spain');
    if ($meta_key = get_post_meta($post->ID,"_as_roomtype",true));
    Code (markup):
    I have to put ; instead of : on ..._as_roomtype",true)); if I put : gives me a error.

    I try to insert the code like this
    <a href="' . get_permalink() . '"><a href="' . get_permalink() . '" class="readnow" ><?php echo $meta_values[$meta_key]; ?></a>
    Code (markup):
    gives me empty spaces.

    [​IMG]

    I try to make like this only to test
    <a href="' . get_permalink() . '"><a href="' . get_permalink() . '" class="readnow" >'.$meta_values.'</a>
    Code (markup):
    and gives me array

    [​IMG]
     
    SharingEngines, Mar 15, 2014 IP
  6. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #6
    A better test would be:

    
    <a href="' . get_permalink() . '"><a href="' . get_permalink() . '" class="readnow" >'.$meta_key.'</a>
    
    PHP:
    What do you get for that? I have a feeling its a case sensitive issue, in which case can be solved by:

    
    <a href="' . get_permalink() . '"><a href="' . get_permalink() . '" class="readnow" ><?php echo $meta_values[strtoupper($meta_key)]; ?></a>
    
    PHP:
     
    ThePHPMaster, Mar 15, 2014 IP