Reputation: 910
I am starting out with PHP for a WordPress and have written some code to put some social network icons in the footer. The way I have done it works, I'm simply calling the content of social network URL stored in the DB and if there is something the icon/link in the footer. But looks very inefficient to me, here is the code, does anyone know how to make it more efficient.
<?php
$social1 = of_get_option('fab_social_twitter_url');
$social2 = of_get_option('fab_social_facebook_url');
$social3 = of_get_option('fab_social_linkedin_url');
?>
<!-- divs for right social network icons column -->
<div class="eight columns">
<div class="social">
<ul>
<?php
if(!empty($social1)) {
?>
<li><a href="<?php echo of_get_option('fab_social_twitter_url'); ?>"><img src="<?php echo of_get_option('fab_social_twitter_icon'); ?>" alt="Follow us on Twitter"></a></li>
<?php
}
?>
<?php
if(!empty($social2)) {
?>
<li><a href="<?php echo of_get_option('fab_social_facebook_url'); ?>"><img src="<?php echo of_get_option('fab_social_facebook_icon'); ?>" alt="Follow us on Facebook"></a></li>
<?php
}
?>
<?php
if(!empty($social3)) {
?>
<li><a href="<?php echo of_get_option('fab_social_linkedin_url'); ?>"><img src="<?php echo of_get_option('fab_social_linkedin_icon'); ?>" alt="Follow us on Linkedin"></a></li>
<?php
}
?>
</ul>
</div>
</div>
Upvotes: 0
Views: 193
Reputation: 10975
Maybe:
<!-- divs for right social network icons column -->
<div class="eight columns">
<div class="social">
<ul>
<?php
foreach (array("twitter","facebook","linkedin") as $option)
($tmp=of_get_option('fab_social_'.$option.'_url')) && (print('<li><a href="'.$tmp.'"><img src="'.of_get_option('fab_social_'.$option.'_icon').'" alt="Follow us on '.ucfirst($option).'"></a></li>'));
?>
</ul>
</div>
</div>
Upvotes: 2
Reputation: 2930
<?php
$social1 = of_get_option('fab_social_twitter_url');
$social2 = of_get_option('fab_social_facebook_url');
$social3 = of_get_option('fab_social_linkedin_url');
$icon1 = of_get_option('fab_social_twitter_icon');
$icon2 = of_get_option('fab_social_facebook_icon');
$icon3 = of_get_option('fab_social_linkedin_icon');
?>
<!-- divs for right social network icons column -->
<div class="eight columns">
<div class="social">
<ul>
<?php if(!empty($social1)) { ?>
<li>
<a href="<?php echo $social1; ?>">
<img src="<?php echo $icon1; ?>" alt="Follow us on Twitter">
</a>
</li>
<?php } ?>
<?php if(!empty($social2)) { ?>
<li>
<a href="<?php echo $social2; ?>">
<img src="<?php echo $icon2; ?>" alt="Follow us on Facebook">
</a>
</li>
<?php } ?>
<?php if(!empty($social3)) { ?>
<li>
<a href="<?php echo $social3; ?>">
<img src="<?php echo $icon3; ?>" alt="Follow us on Linkedin">
</a>
</li>
<?php } ?>
</ul>
</div>
</div>
Upvotes: 0
Reputation: 4808
From a performance point of view, I don't think that there is anything to optimize in here, three separate cases tested individually
Upvotes: 1