Please help me with this code

Discussion in 'PHP' started by Divvy, Sep 18, 2015.

  1. #1
    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                     $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 = ''; }                ?>
    PHP:
    Where you can see this line:
    Below that code I have this one:
    <?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>';                            }                        ?>
    PHP:
    Where you can see this line:
    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:
    <span style="background-position: -0px -0px;" class="icon"></span>
    Code (markup):
    Instead of:
    <img src="//domain.com/wp-content/themes/customtheme/images/favi.png">
    Code (markup):
    Can someone please help me? :)

    Thanks !!!
     
    Divvy, Sep 18, 2015 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    For that, you need to edit the CSS-files (or, at least you should).
    If not, you just need to do this to the span:
    
    <span style="background: url('.$post_favicon_icon.') no-repeat 0 0;" class="icon"></span>
    
    PHP:
    That should work.
     
    PoPSiCLe, Sep 18, 2015 IP
  3. Divvy

    Divvy Well-Known Member

    Messages:
    785
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #3
    Ohhh you are right!! haha I didnt notice the other echo above that one...

    Here what I did:
    
    echo '<span style="background-position: '.$favicon_position[0].'px '.$favicon_position[1].'px;" class="icon"></span>';
    } else {
    echo '<span style="background-position: -0px -0px;" class="icon"></span>';
    
    PHP:
    Thats ok like that right?

    But probably I dont need this code anymore:
    $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/favidef.png';
    PHP:
    Is safe to delete or I can leave like that?

    Thanks once again buddy :)
     
    Divvy, Sep 18, 2015 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #4
    Two things...

    1) Why are you crapping on your markup by inlining CSS?

    2) WHY are you crapping on your markup by inlining CSS?!?

    Ok, I realize these are in fact only one thing, but it's such a colossal thing I felt it warranted being said more than once. 99.99% of the time people use the <style> tag or the style="" attribute, they are doing something WRONG.

    This is really the type of thing you should be simply having classes for to say what these icons ARE, not inlining background-position into the code.
     
    deathshadow, Sep 19, 2015 IP