0

I'm building a wordpress theme. In the backend, the user has the option to enter the url for their social networks (i.e. twitter, facebook, instagram etc). These URL's are then dynamically added to images in the theme front end linking to the respective networks.

The issue I have is that if a user doesn't enter a url for a network, I don't want the image to display. I am trying to write code that says, if the url is blank, echo 'class="hidden"' - this class has display:none in the css.

here is a snippet of my php:

<ul class="icons">
    <li <?php if (get_theme_mod('footer_twitter')==0) {echo 'class="hidden"'; } ?>><a href="<?php echo get_theme_mod('footer_twitter'); ?> " class="icon circle fa-twitter"><span class="label">Twitter</span></a></li>
</ul>

and the css:

ul.icons li.hidden {
   display:none;
}

The php code above currently outputs the echo statement for all cases, even when a url is entered in the backend. Can anyone help me with this code

2
  • echo (empty(get_theme_mod('footer_twitter'))) ? 'class="hidden" : ''; possibly? Commented Jun 22, 2014 at 0:29
  • Afraid not. got the following fatal error: Can't use function return value in write context Commented Jun 22, 2014 at 0:34

2 Answers 2

2

Check the return of "get_theme_mod()" You can check this by using, cause i dont think it "== 0". http://codex.wordpress.org/Function_Reference/get_theme_mod

var_dump(get_theme_mod('footer_twitter'));
//string(0)

Here is your new code:

<ul class="icons">
    <li class="<?php echo empty(get_theme_mod('footer_twitter')) ? 'hidden' : ''; ?>">
        <a href="<?php echo get_theme_mod('footer_twitter'); ?> " class="icon circle fa-twitter">
            <span class="label">Twitter</span>
        </a>
    </li>
</ul>

Please check this Syntax: http://php.net/manual/en/control-structures.alternative-syntax.php its the best way to code control structures in your "View" code.

Sign up to request clarification or add additional context in comments.

8 Comments

So for this to work, do I need to add the var_dump line to the top of my code?
No, var_dump just shows you the explicit return of your function "get_theme_mod()". Ensure its int(0) and not something else. Look at " get_theme_mod('footer_twitter') === 0"
Keep in mind "get_theme_mod('footer_twitter') === 0" is not the same like "get_theme_mod('footer_twitter') == 0". So, in second case, if "get_theme_mod('footer_twitter')" return bool(false) instead of int(0) your condition is "true". cause -> "if (false == 0) { //true }" ;)
This is great, although it appears that it is not adding the class to any. Could it be because the value is a URL not an integer?
Mhmm, can you post the result of var_dump(get_theme_mod('footer_twitter')); ? Please post both, means true/false return.
|
0
<?php if (!empty (get_theme_mod( 'set_address2' ))) {
    echo get_theme_mod( 'set_address2');
}?>

This seemed to work for me in Wordpress. Let me know if this works.

1 Comment

I just saw how long ago this question was asked. I hope I'm not too late.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.