2

I have a global set called socialNetworkContent.

Is there a way to setup a conditional to check whether any of the fields within this set have content?

So the section wouldn't display if all the fields were blank.

Something along these lines:

{% if socialNetworkContent|length %}
<section>
<div class="social">
    <h1>Follow Us</h1>
    <ul>
            {% if socialNetworkContent.branchFacebookProfile|length %}<li><a href="{{ socialNetworkContent.branchFacebookProfile }}"><i class="fa fa-facebook fa-2x"></i></a></li>{% endif %}
            {% if socialNetworkContent.branchTwitterAccount|length %}<li><a href="{{ socialNetworkContent.branchTwitterAccount }}"><i class="fa fa-twitter fa-2x"></i></a></li>{% endif %}
            {% if socialNetworkContent.branchLinkedInAccount|length %}<li><a href="{{ socialNetworkContent.branchLinkedInAccount }}"><i class="fa fa-linkedin fa-2x"></i></a></li>{% endif %}
            {% if socialNetworkContent.branchGooglePlusAccount|length %}<li><a href="{{ socialNetworkContent.branchGooglePlusAccount }}"><i class="fa fa-google-plus fa-2x"></i></a></li>{% endif %}
            {% if socialNetworkContent.branchInstagramAccount|length %}<li><a href="{{ socialNetworkContent.branchInstagramAccount }}"><i class="fa fa-instagram fa-2x"></i></a></li>{% endif %}
            {% if socialNetworkContent.branchYoutubeAccount|length %}<li><a href="{{ socialNetworkContent.branchYoutubeAccount }}"><i class="fa fa-youtube fa-2x"></i></a></li>{% endif %}
    </ul>
</div>
</section>
{% endif %}
2

It's not exactly "quick", you'll have to do something like this:

{% if socialNetworkContent.branchFacebookProfile|length 
       or socialNetworkContent.branchTwitterAccount|length
       or socialNetworkContent.branchLinkedInAccount|length
       ...
%}

Which is probably exactly what you're trying to avoid.

If you want to just make it a little easier to read, you can do something like this:

{% set x = socialNetworkContent %}

{% if x.branchFacebookProfile|length 
       or x.branchTwitterAccount|length
       or x.branchLinkedInAccount|length
       ...
%}

The hurdle that you're dealing with is the fact that the socialNetworkContent object contains many other attributes besides just your custom field names. So there's not really any efficient way to "loop through" your custom fields without dealing with a bunch of extra junk.

Of course, you could build a plugin to create a simple boolean variable, but that sounds like overkill for this situation.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.