Please help me with this small php line

Status
Not open for further replies.

Divvy

Active Member
806
2009
18
0
Hello guys,

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

I have this code:

PHP:
<?php echo get_field( "twitter",$modelID); ?>

If I don't have anything in my "twitter" field, doesnt appear nothing.
If I have something in my "twitter" field appears this code:

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>

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 :)
 
6 comments
Store URL in turl.
Code:
$turl = "https://twitter.com/{$modelid}";

Create link.
Code:
<a href=[B]"<?= $turl?>"[/B] target="_blank"><img src="http://mydomain.com/wp-content/uploads/2016/01/twitter-24.png" alt="Official Twitter" title="Official Twitter"></a>

I don't know PHP so I may be probably be wrong.
 
Last edited:
Ok, in another words:

With the code below Im showing the twitter url:
PHP:
<?php echo get_field( "twitter",$modelID); ?>

Example: https://twitter.com/namehere

But I dont want to show the url, I want to show an image with a link.
I know that I can use this code:

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>

But I dont like that, I want to show the php code with the html together, is it possible? :)
 
Thinking better, I want to use this code:

PHP:
<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>


But I only want to show if my custom field "twitter" is filled.
Is it possible? :)

__________________
Added after 34 minutes:

Solved :D

With the following code:

PHP:
<?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 } ?>

Thank you anyway guys :)
 
Last edited:
Status
Not open for further replies.
Back
Top