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 :)
Related
I am having an issue with my live site that works fine on a dev site (on a different host).
Essentially, it's brining in the image incorrectly and adding characters at the end... example:
https://www.idealhomeloans.com/wp-content/uploads/2016/08/refinance-1.jpg/%3E%20%3Cimg%20src=
it should just be: https://www.idealhomeloans.com/wp-content/uploads/2016/08/refinance-1.jpg
This is the php code I'm using to bring the image in...
<?php if( $image ): ?>
<?php
echo '<img class="'. $imagealigned.'" src="';the_sub_field('image');
echo '/>';
?>
<?php endif; ?>
<?php $image = get_sub_field('image');
//Checking if anything exists for the image field
if ($image) { ?>
<?php echo '<img src="';// display a sub field value
the_sub_field('image');
echo '/>';
?>
<?php } //if there is nothing for image then display
else { ?>
<?php }
?>
Can someone who knows PHP well take a look and see if there's something that would render the image tag to come in correctly?
Any guidance would be greatly appreciated!
Thanks
Try to put values into variables.
<?php if ($image): ?>
<img src="<?= $src; ?>" class="<?= $class; ?>" />
<?php endif; ?>
It is easier to keep the formats and tags of the html, and you could become less confused. Plus I think it is much cleaner.
Perhaps if you actually LOOKED at the html you're generating, you'd see that you're generating BAD html:
echo '<img class="'. $imagealigned.'" src="';the_sub_field('image');
^---start HTML attribute
echo '/>';
^----never end the attribute
So you're building
<img .... src="kittens.jpg>
and around and around the error merrygoround you go...
You are using a semicolon (;) instead of a point (.) to concatenate the_sub_field
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);
I'm working on a new WordPress theme (haven't touched WP themes since like 2.8...) and I'm attempting to make an if/else statement that changes the behavior of my top navigation bar based on whether the page is a front_page or not.
Essentially what I'm after is if the page is a front_page, then display the logo image. If the page is any other kind of page, then display a simple H1 element with the company name:
<li class="name">
<?php
if (is_front_page()) {
echo '<a href="<?php bloginfo("url"); ?>';
echo '<img src="' .bloginfo( "template_directory" ). '/images/logo-dsi.png" alt="DSI" />';
echo '</a>';
} else {
echo '<a href="' .home_url(). '">';
echo '<h1 class="logo">DSI</h1>';
echo '</a>';
}
?>
</li>
The else statement is working fine. But my if statement is not producing the img tag correctly. It echoes the literal bloginfo('template_directory') url and not inserting that into the img src, which is what I want it to do. It's trying to find an image at this url: localhost/images/logo-dsi.png
Obviously my syntax on line 5 is wrong somewhere, but I'm also not sure if I'm going about this the right way. Is there a better solution to what I'm trying to accomplish here?
Thanks!
The later is correct however
echo '<a href="<?php bloginfo("url"); ?>';
Should be
echo '<a href="' . bloginfo("url") . '">';
I tried this out and it worked:
<li class="name">
<a href="<?php bloginfo('url'); ?>">
<?php
if (is_front_page()) { ?>
<img src="<?php bloginfo('template_directory'); ?>/images/logo-dsi.png" alt="DSI" />
<?php
} else { ?>
<h1 class="logo">DSI</h1>
<?php } ?>
</a>
</li>
Apologies, I should have hacked away at it a little longer. But maybe this will become helpful to someone else.
I knew echos in WordPress looked bad for a reason... ;-)
Also I'm sure there's a prettier way to write this out, but for now, that code does the trick.
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 . '';
}
?>
this is my first post for help on here, and man do I really need it. This is the first time I've developed a client's site using multisite, and I'm having trouble applying the appropriate header image to it's site. There are six sites in all, and I'm using the same template for all six sites' front pages. Also, the front page is static and doesn't have a specific page selected.
The conditional below is my attempt at specifying specific images depending on which sub-site I'm on. It keeps throwing a syntax error, (sublime calls it a parse error). I would be so grateful for any help!
<?php
if( get_bloginfo('All in with Laila Ali')) {
<img src="<?php bloginfo('template_directory');?>/images/Banner-LailaAli.jpg" />
} elseif{
if( get_bloginfo('Jaimies 15 Minute Meals')) {
<img src="<?php bloginfo('template_directory');?>/images/Banner-JamieOliver.jpg" />
}
} elseif{
if( get_bloginfo('Lucky Dog')) {
<img src="<?php bloginfo('template_directory');?>/images/Banner-LuckyDog.jpg" />
}
} elseif{
if( get_bloginfo('Game Changers with Kevin Frazier')) {
<img src="<?php bloginfo('template_directory');?>/images/Banner-GameChangers.jpg" />
}
} elseif{
if( get_bloginfo('Recipe Rehab')) {
<img src="<?php bloginfo('template_directory');?>/images/Banner-RecipeRehab.jpg" />
}
} else {
<img src="<?php bloginfo('template_directory');?>/images/Banner-PetVet.jpg" />
}
?>
You are getting this error because you have consecutive <?php tags with HTML in between them. Things like <img src=" aren't valid php, but you are inside <?php tags, so PHP gives you an error.
One of the ways you can fix this is by ending the tags when you need to switch back to HTML. Like this:
<?php
if( get_bloginfo('All in with Laila Ali')) {
?>
<img src="<?php bloginfo('template_directory');?>/images/Banner-LailaAli.jpg" />
<?php
} elseif{
if( get_bloginfo('Jaimies 15 Minute Meals')) {
?>
<img src="<?php bloginfo('template_directory');?>/images/Banner-JamieOliver.jpg" />
<?php
}
}
?>
Another way, is to have your PHP echo the HTML you want. Which would look something like this:
<?php
if( get_bloginfo('All in with Laila Ali')) {
echo '<img src="' . bloginfo('template_directory') . '/images/Banner-LailaAli.jpg" />';
} elseif{
if( get_bloginfo('Jaimies 15 Minute Meals')) {
echo '<img src="'. bloginfo('template_directory') . '/images/Banner-JamieOliver.jpg" />';
}
}
?>
Basically, you have to remember that once you open a <?php tag, you can only put valid PHP until you close it with ?>.