• Hello.

    I am trying to figure out an if/else statement for the following scenario:

    echo get_post_meta($postid, ‘Alabama’, true) {
    echo “, Alabama”;
    } else {
    if (echo get_post_meta($postid, ‘Alaska’, true) {
    echo “, Alaska”;

    Basically, a user is able to select State from a dropdown list. Then, an associated dropdown field, city, would be displayed for user to select a city for the state.

    I created custom fields for Alabama and Alaska so that user can select associated city.

    The output I’m trying to achieve is:

    Location: Mobile, Alabama

    after the user selects State and City.

    My question is: can somebody kindly advise on how to integrate an If Else statement for the above php code? The Alabama php code outputs correctly. However, I can’t seem to figure out how to include other states and associated city for the user.

    Hope that made sense. Greatly appreciated your assistance folks! https://www.ymphony.com

Viewing 7 replies - 1 through 7 (of 7 total)
  • i think you need to use ajax for what you need.
    this is an example in ajax

    Thread Starter riseUp

    (@mosesyoon)

    Hello. I looked at the ajax link, but I’m not sure if that will work.

    Currently, I am almost close. I am using the following code:

    get_post_meta( $postid, ‘alabama’, true) ;
    echo get_post_meta($postid, ‘alabama’, true);
    echo “, Alabama”;

    get_post_meta( $postid, ‘alaska’, true);
    echo get_post_meta($postid, ‘alaska’, true);
    echo “, Alaska”;

    However, folks, how do I make the above 2 sets of php code into an if – else statement? If you view https://www.ymphony.com, you will notice that the Location: field is still a little off.

    Can somebody advise how to apply an if else statement for the above 2 pieces of code?

    Thank you!

    Hi riseUp,

    As per your code, I suppose that you save the city of Alaska in alaska meta key and you want to show the city with state like : City Name, Alaska

    Try this code

    if(get_post_meta( $postid, 'alabama', true))
    {
          echo get_post_meta($postid, 'alabama', true);
          echo ", Alabama";
    }
    if(get_post_meta( $postid, 'alaska', true))
    {
          echo get_post_meta($postid, 'alaska', true);
          echo ", Alaska";
    }

    Hope it will fulfill your need.
    Thanks

    Thread Starter riseUp

    (@mosesyoon)

    Hi @cedcommerce,

    We are almost there. Thanks for the code update.

    If you check out https://www.ymphony.com homepage, you will notice the following displayed in the latest post:

    Location: Auburn, AlabamaJuneau, Alaska

    It does not seem like the if statements executed as both sets of city, state combinations are displaying. It should be displayed as:

    Location: Auburn, Alabama or Location: Juneau, Alaska

    Can you advise on an update to the php code so that the if else statements function properly? Thank you!

    Hi riseUp,
    Put the second if condition within else and you reach to destination ??

    if(get_post_meta( $postid, 'alabama', true))
    {
          echo get_post_meta($postid, 'alabama', true);
          echo ", Alabama";
    }
    elseif(get_post_meta( $postid, 'alaska', true))
    {
          echo get_post_meta($postid, 'alaska', true);
          echo ", Alaska";
    }

    Thanks

    Thread Starter riseUp

    (@mosesyoon)

    Hi @cedcommerce,

    Still not working. Looks like the city, state is not displaying. Any other thoughts? Thank you!

    Thread Starter riseUp

    (@mosesyoon)

    And this also is not working:

    if(get_post_meta( $postid, ‘state’, true))
    {
    echo get_post_meta($postid, ‘cityalabama’, true);
    echo “, Alabama”;
    }
    elseif(get_post_meta( $postid, ‘state’, true))
    {
    echo get_post_meta($postid, ‘cityalaska’, true);
    echo “, Alaska”;
    }

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to use echo get_post_meta() in if/else statement’ is closed to new replies.