1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Wordpress - replace variable with a name

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

  1. #1
    Hi,

    I have tis code

    Code (Text):
    1. <'.if (get_post_meta($post->ID,"_as_roomtype",true) == 'DE').'>
    2. <a href="'.get_permalink().'" class="readnow" >"---NAME---"</a>
    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 Well-Known Member

    Messages:
    1,754
    Likes Received:
    177
    Best Answers:
    66
    Trophy Points:
    160
    #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:
    PHP:
    1.  
    2. $countries = array(0 => array(1 => 'DE', 2=> 'Germany'), 1 => array(1 => 'UK', 2 => 'United Kingdom'), 2 => array(1 => 'SP', 2 => 'Spain'));
    3.  
    and then do something like this:
    PHP:
    1.  
    2. for ($i = 0; $i < count($countries); $i++) {
    3. echo '<'.if (get_post meta($post->ID,"_as_roomtype",true) == $countries[$i][1]).'>
    4. <a href="'.get_pemalink().'" class="readnow">'.$countries[$i][2].'</a>';
    5. }
    6.  
    I think that should work, haven't tested it though
     
    PoPSiCLe, Mar 15, 2014 IP
  3. HassanKhalid

    HassanKhalid Active Member

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

    PoPSiCLe Well-Known Member

    Messages:
    1,754
    Likes Received:
    177
    Best Answers:
    66
    Trophy Points:
    160
    #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

    Code (Text):
    1. $meta_values = array('DE' => 'Germany', 'UK' => 'United Kingdom', 'SP' => 'Spain');
    2. if ($meta_key = get_post_meta($post->ID,"_as_roomtype",true));
    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
    Code (Text):
    1. <a href="' . get_permalink() . '"><a href="' . get_permalink() . '" class="readnow" ><?php echo $meta_values[$meta_key]; ?></a>
    gives me empty spaces.

    [​IMG]

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

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

    ThePHPMaster Well-Known Member

    Messages:
    624
    Likes Received:
    32
    Best Answers:
    20
    Trophy Points:
    100
    #6
    A better test would be:

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

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