I wonder if you can help me slim down my code.
I've built a bespoke share include for a client site. It works fine but I'm sure one of you clever lot can help me to make it less bulky.
I have three share URLs:
$share_url_facebook, $share_url_twitter and $share_url_linkedin.
Each URL is different in its structure and is fairly complex.
I then have the following code:
echo '<a href="' . $share_url_facebook . '" title="Share this on facebook">';
echo '<i class="fa fa-facebook"></i>';
echo '</a>';
echo '<a href="' . $share_url_twitter . '" title="Share this on twitter">';
echo '<i class="fa fa-twitter"></i>';
echo '</a>';
echo '<a href="' . $share_url_linkedin . '" title="Share this on LinkedIn">';
echo '<i class="fa fa-linkedin"></i>';
echo '</a>';
Is there a way for me to create some sort of loop to prevent the repetition?
Based on the accepted solution by @JasonK here is my final(simplified) tested code:
$share = [
'facebook' => [
'title' => 'Facebook',
'url' => 'https://www.facebook.com/'
],
'twitter' => [
'title' => 'Twitter',
'url' => 'https://twitter.com'
],
'linkedin' => [
'title' => 'LinkedIn',
'url' => 'https://linkedin.com'
]
];
foreach ($share as $key => $details) {
echo '<a href="' . $details['url'] . '" title="Share this on ' . $details['title'] . '">';
echo '<i class="fa fa-' . $key . '"></i>';
echo '</a>';
}
$share_url_???andtitleand the css class namesfa-????so you might even end up with more code in an attempt to create this array and then loop over it$shareBtns = ['socialMediaName' => 'url', ...]and iterate over that.