Please help me with this small php line

Discussion in 'PHP' started by Divvy, Jan 20, 2016.

  1. #1
    Hello guys,

    Maybe someone can help me with this small peace of code :)

    I have this code:

    <?php echo get_field( "twitter",$modelID); ?>
    PHP:
    If I don't have anything in my "twitter" field, doesnt appear nothing.
    If I have something in my "twitter" field appears this code:

    <a href="#" target="_blank"><img src="http://mydomain.com/wp-content/uploads/2016/01/twitter-24.png" alt="Official Twitter" title="Official Twitter"></a>
    Code (markup):
    Everything is working fine here, but I like to ask if is possible to integrate the html code in the first line of php above :)

    The idea is to show twitter url using the first php code and replace the # in the href with the twitter url.

    I know that can be difficult to understand what I want lol, but if you have trouble understanding or if you have any doubt, please let me know and I will try to explain with another words.

    Hope someone can help me, thank you guys :)

    ----------------------------------------------------------------------------------------------

     
    Last edited: Jan 20, 2016
    Divvy, Jan 20, 2016 IP
  2. Divvy

    Divvy Well-Known Member

    Messages:
    785
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #2
    Thinking better, I want to use this code:

    
    <a href="<?php echo get_field( "twitter",$modelID); ?>" target="_blank"><img src="http://mydomain.com/wp-content/uploads/2016/01/twitter-24.png" alt="Official Twitter" title="Official Twitter"></a>
    PHP:
    But I only want to show if my custom field "twitter" is filled.
    Is it possible? :)
     
    Divvy, Jan 21, 2016 IP
  3. Divvy

    Divvy Well-Known Member

    Messages:
    785
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #3
    Solved :D

    With the following code:

    <?php if ( get_post_meta($post->ID, 'twitter', true) ) { ?><a target="_blank" href="<?php echo get_post_meta($post->ID, "twitter", $single = true); ?>"><img src="http://mydomain.com/wp-content/uploads/2016/01/twitter-24.png" alt="<?php the_title(); ?> Official Twitter" title="<?php the_title(); ?> Official Twitter" /></a><?php } ?>
    PHP:
    Thank you anyway guys :)
     
    Divvy, Jan 21, 2016 IP