Please help me with this code

Status
Not open for further replies.

Divvy

Active Member
806
2009
18
0
Hello guys,

Can someone help me with a little thing?
I dont have too much php knowledges, and I have sure that someone with good knowledges can fix this for me in 2 minutes :)

Let me try to explain:

I have this code:
PHP:
<?php 					$post_favicon_icon = (get_post_meta(get_the_ID(),'post_favicon_icon',true))?get_post_meta(get_the_ID(),'post_favicon_icon',true):get_template_directory_uri().'/images/favi.png';					$post_external_link = (get_post_meta(get_the_ID(),'post_external_link',true))?get_post_meta(get_the_ID(),'post_external_link',true):'#';					$new_post_added = (get_post_meta(get_the_ID(),'new_post_added',true))?'<span class="new_txt">'.get_post_meta(get_the_ID(),'new_post_added',true).'</span>':'';										if(get_field( "nofollow_tag" )) { $nofollow_tag = 'rel="nofollow"'; }					else { $nofollow_tag = ''; }				 ?>

Where you can see this line:
$post_favicon_icon = (get_post_meta(get_the_ID(),'post_favicon_icon',true))?get_post_meta(get_the_ID(),'post_favicon_icon',true):get_template_directory_uri().'/images/favi.png';

Below that code I have this one:
PHP:
<?php							 if(get_field( "add_site_favicon_position" )) {								$favicon_position = explode(',', get_field( "add_site_favicon_position" ));								echo '<span style="background-position: '.$favicon_position[0].'px '.$favicon_position[1].'px;" class="icon"></span>';							} else {								echo '<span><img src="'.$post_favicon_icon.'" /></span>';							}						?>

Where you can see this line:
echo '<span><img src="'.$post_favicon_icon.'" /></span>';

This code will show an default image "images/favi.png", but when I upload a image (favicon), will show the one that I uploaded.
Everything well here...

But I don't want to show the default image in a img src tag... I want to show in a sprite image using span.
This is the code that I want to show for default image only:
Code:
<span style="background-position: -0px -0px;" class="icon"></span>

Instead of:
Code:
<img src="//domain.com/wp-content/themes/customtheme/images/favi.png">

Can someone please help me? :)

Thanks !!!
 
4 comments
Thank you for your reply buddy, I also thought on that too but Im afraid that If I do that, will always appear the default image, and not the one that I upload...
I have an "else" in the code:

get_post_meta(get_the_ID(),'post_favicon_icon ',true):get_template_directory_uri().'/images/favi.png'


With this variable: $post_favicon_icon
Will appear one or other...
 
Try this:
PHP:
 <?php
$post_external_link = (get_post_meta(get_the_ID(), 'post_external_link', true)) ? get_post_meta(get_the_ID(), 'post_external_link', true) : '#';
$new_post_added     = (get_post_meta(get_the_ID(), 'new_post_added', true)) ? '<span class="new_txt">' . get_post_meta(get_the_ID(), 'new_post_added', true) . '</span>' : '';
if (get_field("nofollow_tag")) {
    $nofollow_tag = 'rel="nofollow"';
} else {
    $nofollow_tag = '';
}
?> 

<?php
if (get_field("add_site_favicon_position")) {
    $favicon_position = explode(',', get_field("add_site_favicon_position"));
    echo '<span style="background-position: ' . $favicon_position[0] . 'px ' . $favicon_position[1] . 'px;" class="icon"></span>';
} else {
    if (get_post_meta(get_the_ID(), 'post_favicon_icon', true)) {
        echo '<span><img src="' . get_post_meta(get_the_ID(), 'post_favicon_icon', true) . '" /></span>';
    } else {
        echo '<span style="background-position: -0px -0px;" class="icon"></span>';
    }
}
?>

I have no idea whats the use of field "add_site_favicon_position". But if it doesn't exists then it should do as you described.
 
Status
Not open for further replies.
Back
Top