Please help me with "else"

Discussion in 'PHP' started by Divvy, Sep 16, 2015.

  1. #1
    I need some little help, if someone can help me I really appreciate :)

    I have this code:
    <?php echo $post_external_link = (get_post_meta(get_the_ID(),'post_external_link',true))?'<a href="'.get_post_meta(get_the_ID(),'post_external_link',true).'" '.$nofollow_tag.'>'.get_post_meta(get_the_ID(),'post_external_link',true).'</a>':'#'; ?>
    PHP:
    I want to display:
    '.get_post_meta(get_the_ID(),'post_external_link',true).'
    Code (markup):
    Or else:
    '.get_post_meta($post->ID, 'domaintag', true).'
    Code (markup):
    How I put in the code?
    I tried this but didn't worked:
    Can someone help me? Thanks! :)
     
    Solved! View solution.
    Divvy, Sep 16, 2015 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    What you have there is a short-form if / else already.
    First bit is the
    (get_post_meta(get_the_ID(),'post_external_link',true))
    This is the if-statement, followed by a ? (question mark) for what is shown if that condition is true, namely here:
    '<a href="'.get_post_meta(get_the_ID(),'post_external_link',true).'" '.$nofollow_tag.'>'.get_post_meta(get_the_ID(),'post_external_link',true).'</a>'
    On the end, you have a : (colon) followed by a '#';
    I'm not entirely sure what that # is there for, but if it's just some form of placeholder, you can add the code in your ELSE after that : (colon) (instead of the '#'
     
    PoPSiCLe, Sep 16, 2015 IP
  3. Divvy

    Divvy Well-Known Member

    Messages:
    785
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #3
    Hey buddy, thank you again for your help!

    Unfortunately my php knowledge is not so good... can you help me if I give you full code of the file?
    http://paste2.org/J2saOL6O

    Thanks :)
     
    Divvy, Sep 16, 2015 IP
  4. Divvy

    Divvy Well-Known Member

    Messages:
    785
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #4
    Btw, I tried this code:

    <?php echo $post_external_link = (get_post_meta(get_the_ID(),'post_external_link',true))?'<a href="'.get_post_meta(get_the_ID(),'post_external_link',true).'" '.$nofollow_tag.'>'.get_post_meta($post->ID, 'domaintag', true).'</a>' : get_post_meta(get_the_ID(),'post_external_link',true); ?>
    PHP:
    first domaintag and second post_external_link... but didnt worked :(
    Only shows domaintag...
     
    Divvy, Sep 16, 2015 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    You need to copy all of it, including the <a> tag.

    Something like this:
     
    <?php echo $post_external_link = (get_post_meta(get_the_ID(),'post_external_link',true))?'<a href="'.get_post_meta(get_the_ID(),'post_external_link',true).'" '.$nofollow_tag.'>'.get_post_meta($post->ID, 'post_external_link', true).'</a>' : '<a href="'.g
    et_post_meta($post->ID, 'domaintag', true)
    .'>'.get_post_meta($post->ID, 'domaintag', true).'</a>';
    
    PHP:
    Typed this on my phone, so there might be errors
     
    PoPSiCLe, Sep 16, 2015 IP
  6. Divvy

    Divvy Well-Known Member

    Messages:
    785
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #6
    Hey buddy, thank you for your reply and for trying to help me again.

    I tried that but didnt worked.... the code that I used:
    <?php echo $post_external_link = (get_post_meta(get_the_ID(),'post_external_link',true))?'<a href="'.get_post_meta(get_the_ID(),'post_external_link',true).'" '.$nofollow_tag.'>'.get_post_meta($post->ID, 'domaintag', true).'</a>':'<a href="'.get_post_meta(get_the_ID(),'post_external_link',true).'" '.$nofollow_tag.'>'.get_post_meta(get_the_ID(),'post_external_link',true).'</a>'; ?>
    PHP:
    Only the first <a> is working... after the : is not working... but if I switch 2nd <a> with the first, worked fine :/

    Full code: http://paste2.org/ZAkb6GUj
     
    Divvy, Sep 17, 2015 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    I'm a little confused - first you check get_the_ID() with 'post_external_link' and true - if that check returns true (what comes directly after the question mark) you load the same (that you just checked) in the HREF of the a-tag, but for some reason you're using another get_post_meta as the link name - which I don't get. Wouldn't the smart thing be to use the same get_post_meta for both the HREF and the link name?
     
    PoPSiCLe, Sep 17, 2015 IP
  8. Divvy

    Divvy Well-Known Member

    Messages:
    785
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #8
    Ok, I'm going to explain better what I want to do, I think that is better for a better understanding :)

    With this code:
    <?php echo $post_external_link = (get_post_meta(get_the_ID(),'post_external_link',true))?'<a href="'.get_post_meta(get_the_ID(),'post_external_link',true).'" '.$nofollow_tag.'>'.get_post_meta(get_the_ID(),'post_external_link',true).'</a>':'#'; ?>
    PHP:
    Appears like this:
    [​IMG]

    And with this code:
    <?php echo $post_external_link = (get_post_meta(get_the_ID(),'post_external_link',true))?'<a href="'.get_post_meta(get_the_ID(),'post_external_link',true).'" '.$nofollow_tag.'>'.get_post_meta($post->ID, 'domaintag', true).'</a>':'#'; ?>
    PHP:
    Appears like this:
    [​IMG]

    I want to show always the 1st code get_post_meta(get_the_ID(),'post_external_link',true)
    But If custom field is added, I want to appear custom field first: get_post_meta($post->ID, 'domaintag', true)
    So I need href to be the same, but only "title" changes...

    [​IMG]

    Can you help me? :)
     
    Last edited: Sep 17, 2015
    Divvy, Sep 17, 2015 IP
  9. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #9
    Aha. My bad, I misunderstood what you wanted.
    
    <?php echo $post_external_link = '<a href="'.get_post_meta(get_the_ID(),'post_external_link',true).'" '.$nofollow_tag.'>'.(get_post_meta(get_the_ID(),'post_external_link',true) ? get_post_meta(get_the_ID(),'post_external_link',true) : get_post_meta($post->ID, 'domaintag', true)).'</a>'; ?>
    
    PHP:
    This should in theory do what you want - I've moved the comparison / difference into only the visible link-part, instead of the whole code (the <a>-tag stays the same, only the visible link changes, as I understood you wanted it.
     
    PoPSiCLe, Sep 17, 2015 IP
  10. Divvy

    Divvy Well-Known Member

    Messages:
    785
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #10
    Thank you again for your help buddy, but unfortunately didnt worked ;(
    Sorry for waste your time with this...

    I added your code and the url that appeared was the given by get_post_meta(get_the_ID(),'post_external_link',true):
    [​IMG]

    and in this one should appeared other one because I have custom field in there:
    [​IMG]

    Any more ideas? :)
     
    Divvy, Sep 17, 2015 IP
  11. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #11
    Hm - I'm thinking that it will ALWAYS equate to true about the post_external_link - ie, that will always be what triggers, it will never get to the else-part, even if you use that custom tag.
    Try switching the two checks around - ie, check to see if the domaintag-one is present, and if it is, show that - if not, show the post_external_link.
    Basically, change the check (the content within the first ()) and switch the two results with eachother.
     
    PoPSiCLe, Sep 18, 2015 IP
  12. Divvy

    Divvy Well-Known Member

    Messages:
    785
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #12
    Hey buddy, thank you once again!! :)

    My php knowledge is very poor... so I didnt understood too well what you tried to say... Will give you lots of work to write that code for me? :)

    Thank you once again!
     
    Divvy, Sep 18, 2015 IP
  13. #13
    Try this one:
    
    <?php echo $post_external_link = '<a href="'.get_post_meta(get_the_ID(),'post_external_link',true).'" '.$nofollow_tag.'>'.(get_post_meta($post->ID,'domaintag',true) ? get_post_meta($post->ID, 'domaintag', true) _ get_post_meta(get_the_ID(),'post_external_link',true)).'</a>'; ?>
    
    PHP:
    Granted, this assumes that the check for domaintag is correct
     
    PoPSiCLe, Sep 18, 2015 IP
  14. Divvy

    Divvy Well-Known Member

    Messages:
    785
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #14
    Yeahhhhh is finally working !!! Thank you bro !!! :D

    When I added the code appeared an error, after checking the code I notice this:
    
    ? get_post_meta($post->ID, 'domaintag', true) _ get_post_meta(get_the_ID(),'post_external_link',true)).
    PHP:
    I replaced the "_" with the ":" and worked :)

    Thanks to you!! Thank you once again!!!
     
    Divvy, Sep 18, 2015 IP
  15. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #15
    Ops - sorry about that :) Probably went a little fast, and I missed the correct button. Glad you got it working.
     
    PoPSiCLe, Sep 18, 2015 IP