Hide php link if the field is empty? - php

I'm trying to display a link only if it has a value.
How can i get the image if the_field imdb is not empty?
<a href="<?php the_field('imdb'); ?>" >
<img style="width:60px;"src="/img/link.png" /></a>

Use get_field(); instead of the_filed();.
if(!empty(get_field("fildname"))){
#your code hear
}

Try:-
if(!empty(the_field('imdb'))){
<a href="<?php the_field('imdb'); ?>" ><img style="width:60px;"src="/img/link.png" /></a>
}

Use isset
if(isset(the_field('imdb'))&&!empty(the_field('imdb'))){
<img src=""/>
}

Since i didnt want to show the img or link on older posts i solved it by:
<?php
// assign image
$imdbimg = "<img src='http://mydomain.com/IMDb.png' class='imdb' />";
// imdb link from custom field
$imdblink = get_field('imdb');
?>
// display img and link on post newer then id 16
<?php global $post;
if ( $post->ID >= 16 ){
echo ' ' . $imdbimg . '';
}
?>

Related

I'm trying to echo an image in a PHP variable in CodeIgniter

Hello I'm trying to echo an image on a view page in CodeIgniter but nothing is displayed on the page.
Here I'm making the variable:
<?php $image = "<img src='../../images/stoel.jpg' alt='img' />"; ?>
And here I'm trying to echo the image:
<img src="<?php echo $image;?>">
Intro
This is the most basic php, so what's the addition of this question? Please read the basics of echo here: http://php.net/manual/en/function.echo.php (Example 1).
Learn the basics first!
Solution
1. Assign full html tag to variable and echo full html:
<?php
$image = '<img src="../../images/stoel.jpg" alt="Foo">';
echo $image;
?>
2. Or assign image path to variable and echo concat string:
<?php
$path = '../../images/stoel.jpg';
echo '<img src="' . $path . '" alt="Foo">';
?>
3. Or assign image path to variable and echo only this with php:
<?php
$path = '../../images/stoel.jpg';
?>
<img src="<?= $path; ?>" alt="Foo">
You are inserting full Image tag into src attribute.
<?php $image = "<img src='../../images/stoel.jpg' alt='img' />"; ?>
and
<?php echo $image; ?>
or
<?php $image = "../../images/stoel.jpg"; ?>
and
<img src="<?php echo $image;?>">
<?php $image = "<img src='../../images/stoel.jpg' alt='img' />"; ?>
you try like this
<?php echo $image; ?>
You are already assign full image code in variable so you just need to print your variable:
<img src="<?php echo $image;?>">
To
<?php echo $image;?>
Make sure your images out side of the application folder then you can do something like
application
images
images > stoel.jpg
system
index.php
Use Base url from the url helper
<img src="<?php echo base_url('images/stoel.jpg');?>" />
Make sure you have set the base_url in config.php
$config['base_url'] = 'http://localhost/yourproject/';
You can also use HTML Helper img();
I think this is what you mean. Since you are echoing inside the src attribute, you do not need to store the whole <image>, just the path will do.
<?php
$image = "../../images/stoel.jpg";
?>
<img src="<?php echo $image;?>">

WordPress links to social icons not linking

I'm attempting to add social follow icons to a BuddyPress site using a section of code that I found here:
https://buddypress.org/support/topic/display-users-social-follow-buttons-in-profile/
I followed the suggestion downthread and added the code to a bp-custom.php file in my plugins directory and the icons are showing up where they should but the link to the social profile is showing as text and when I click on the link it returns a 404 page.
I'm sure I have something wrong but I'm just too clueless new to spot it.
<?php
//Social Media Icons based on the profile user info
function member_social_extend(){
$dmember_id = $bp->displayed_user->id;
$fb_info = xprofile_get_field_data('facebook', $dmember_id);
$google_info = xprofile_get_field_data('googleplus', $dmember_id);
$linkedin_info = xprofile_get_field_data('linkedin', $dmember_id);
$twitter_info = xprofile_get_field_data('twitter', $dmember_id);
echo '<div class="member-social">';
if($fb_info||$google_info||$linkedin_info||$twitter_info){
echo 'My Social: ';
}
if ($fb_info) {
?>
<span class="fb-info"><img src="<?php bloginfo('wpurl'); ?>/wp-content/themes/family-openstrap-child/images/facebook.png" /></span>
<?php
}
?>
<?php
if ($google_info) {
?>
<span class="google-info"><img src="<?php bloginfo('wpurl'); ?>/wp-content/themes/family-openstrap-child/images/googleplus.png" /></span>
<?php
}
?>
<?php
if ($linkedin_info) {
?>
<span class="linkedin-info"><img src="<?php bloginfo('wpurl'); ?>/wp-content/themes/family-openstrap-child/images/linkedin.png" /></span>
<?php
}
?>
<?php
if ($twitter_info) {
?>
<span class="twitter-info"><img src="<?php bloginfo('wpurl'); ?>/wp-content/themes/family-openstrap-child/images/twitter.png" /></span>
<?php
}
echo '</div>';
}
add_filter( 'bp_before_member_header_meta', 'member_social_extend' );
?>
Thanks!
Looks like xprofile_get_field_data() creates its own <a> tag, so you don't have to write one out like you've done there. Try this instead:
$fb_info = xprofile_get_field_data(
'<img src="' . bloginfo('wpurl') . '/wp-content/themes/family-openstrap-child/images/facebook.png" />',
$dmember_id
);
...
<span class="fb-info"><?php echo $fb_info; ?></span>
Not sure if xprofile_get_field_data() will let you pass HTML in the first parameter there, so if that doesn't work quite right, you could fall back to something simpler (no image) like:
$fb_info = xprofile_get_field_data('Facebook', $dmember_id);

Show image, if it's set in database

I'm trying to show an image (or rather a link to an image) stored in a database and I'd like to get the image to show only if the link is set in the database.
Currently, if the link is not set (value is null), it shows a broken link.
Is there a way to for example use an if-statement and echo a HTML-code?
Something like this:
(The value have been fecthed to array $current in this example:)
<?php
if(isset($current['image']) {
echo "<img src='<?php echo $current['image'];
?>' class='left' style='max-height:20em; max-width:15em; margin-right:1em; margin-top:0;'})">
You used <?php twice, you have problem with quotes, brackets, etc.
<?php
if (!empty($current['image'])) {
echo "<img src='" . $current['image'] . "' class='left' style='max-height:20em; max-width:15em; margin-right:1em; margin-top:0;'>";
} else {
// here you can write for example default no-image image or whatever yo want, if you want
}
Nevermind, got it.
-Solution:
<?php if(isset($current['image'])): ?><img src="<?php echo $current['image']; ?>" class="left" style="max-height:20em; max-width:15em; margin-right:1em; margin-top:0;})">
<?php endif; ?>
<?php
if(isset($current['image'])) {
?>
<img src='<?php echo $current['image'];?>' class='left' style='max-height:20em; max-width:15em;
margin-right:1em; margin-top:0;'>
<?php
}
?>

PHP - html image link displayed as text

I am new in this so I'll try to be clear as I can.
I want to display\output, using php, page an image link html tag if user filed not empty like this on client side:
<a href="[dynamic from user filed]" title="My facebook">
<img src="images/facebook.png" alt="facebook" /></a>
so I wrote this but it always displays on html (client) the filed data as text without the all the html code (no html on client):
<div>
<?php
$Usr_Url = bp_member_profile_data('field=facebook' );
if ( !empty( $Usr_Url )) { ?>
<a href="<?php $Usr_Url; ?>" title="My facebook">
<img src="images/facebook.png" alt="facebook" /></a>
<?php } ?>
</div>
I suppose it is security issue so I need to make the code a server side code or something, can you give an advice please?
the current output seems to be always the user filed on html: www.facebook.com/try
*(need to add the I am retrieving buddypress field bp_member_profile_data(filed='filedname') and that sections works)
You are doing it the wrong way. Do it like this:
<?php
$Usr_Url = bp_member_profile_data('field=facebook' );
if ( !empty( $Usr_Url ))
{
echo "<a href=".$Usr_Url."title='My facebook' <img src='images/facebook.png' alt='facebook' /></a>";
}
?>
Comment if there are errors.
If wanting to output text from inside a PHP code block you need to use echo:
<?php
$Usr_Url = 'http://example.com';
echo 'Example URL';
...
?>
Or alternatively, you can mix in PHP code blocks into your HTML:
<?php $Usr_Url = 'http://example.com'; ?>
Example URL
...
You can't just stick HTML elements into your PHP code blocks though, as this breaks the syntax as in the original post.
To rework your code:
<div>
<?php
$Usr_Url = bp_member_profile_data('field=facebook' );
if ( !empty( $Usr_Url )) {
echo '<a href="'.$Usr_Url.'" title="My facebook">';
echo '<img src="images/facebook.png" alt="facebook" /></a>';
} ?>
</div>
You can try this out. It displays the image when Usr_Url is NOT empty:
<?php
$Usr_Url = bp_member_profile_data('field=facebook' );
if ( $Usr_Url != "" ) {
echo '<img src="images/facebook.png" alt="facebook" />'; } else { echo '$Usr_Url is empty...'; }
?>
I hope it helps :)

PHP echo function inside a HTML link

I have a PHP echo function inside of a HTML link, but it isn't working. I want to have an image location, defined in img src, be in part of the clickable link of the image. The page will have multiple images doing the same thing, so I am trying to use PHP to automate this.
<a href="http://statuspics.likeoverload.com/<?php echo $image; ?>">
<img src="<?php $image=troll/GrannyTroll.jpg?>" width="100" height="94" />
</a>
Turn
<?php $image=troll/GrannyTroll.jpg?>
into
<?php echo "troll/GrannyTroll.jpg"; ?>
?
Or provide more details on what you are trying to achieve.
Also, you might consider urlencode-ing some of those URL parameters.
Edit:
So you might try setting the variable beforehand:
<?php $image = "troll/GrannyTroll.jpg"; ?>
<img src="<?php echo $picture; ?>" width="100" height="94" />
So now i understand what you are trying to do.
One error is that you didn't enclose $image=troll/GrannyTroll.jpg with quotes like this:
$image = 'troll/GrannyTroll.jpg';
The second error is that you do it in the wrong order, you have to define $image first, before you use it.
That's what I believe you want to do:
<?php
$image = "troll/GrannyTroll.jpg";
?>
<img src="<?php echo $image; ?>" width="100" height="94"/>

Categories