Is there a better way to do this other than just adding in a new condition each time I want to add a social link?
<?php if($site->socialfacebook()->isNotEmpty() || $site->socialtwitter()->isNotEmpty() || $site->sociallinkedin()->isNotEmpty() || $site->socialinstagram()->isNotEmpty() || $site->socialyoutube()->isNotEmpty() || $site->socialsteam()->isNotEmpty() || $site->socialtwitch()->isNotEmpty() || $site->socialskype()->isNotEmpty()): ?>
Suggestion:
<?php
// create an array of fields
$fields = ['socialfacebook', 'socialblabla...'];
// loop through fields
foreach($fields as $field)
{
if($site->{$field}()->isNotEmpty())
{
// do stuff
}
}
?>
Please use the “Question” category for questions. Makes it easier to find open question.
1 Like
And how would I get html in the do stuff section? Echo doesn’t seem to work.
I will mark all my future questions as questions
Thanks for pointing it out to me.
EDIT
I got it
<?php
$fields = ['socialfacebook', 'socialtwitter', 'sociallinkedin', 'socialinstagram', 'socialyoutube', 'socialsteam', 'socialtwitch', 'socialskype']; ?>
<?php foreach($fields as $field): ?>
<?php if($site->{$field}()->isNotEmpty()): ?>
<li class="menu-navigation__item">
<a href="<?= $site->{$field}() ?>" class="menu-navigation__link">
<svg class="icon">
<use xlink:href="#icon-<?= substr($field, 6) ?>" />
</svg>
</a>
</li>
<?php endif ?>
<?php endforeach ?>
See I’m slowly learning 
Thank you senpai “texnixe”
Hm, almost. Your current code will only output a link to Facebook though, you have to use the field placeholder in the href attribute and get the correct icon.
Edit: See code above.
Already updated that part 
EDIT
The final code for anyone that wants it
<?php
$fields = ['socialfacebook', 'socialtwitter', 'sociallinkedin', 'socialinstagram', 'socialyoutube', 'socialsteam', 'socialtwitch', 'socialskype'];
?>
<ul class="social-links social-links--white menu-navigation menu-navigation--horizontal">
<?php foreach($fields as $field): ?>
<?php if($site->{$field}()->isNotEmpty()): ?>
<?php $icon = substr($site->{$field}()->key(), 6); ?>
<li class="menu-navigation__item">
<a href="<?= $site->{$field}() ?>" class="menu-navigation__link">
<svg class="icon">
<use xlink:href="#icon-<?= $icon ?>" />
</svg>
</a>
</li>
<?php endif ?>
<?php endforeach ?>
</ul>
That’s another way of doing it, but you already have the name of the field in the $field
variable, so no need to get it again, see my edited code above.
Ah I see, OK, that makes sense.