我创建了一个WordPress插件,用于对社交媒体配置文件进行排序,并按订单号输出。我有一个问题,如果我在页面上插件超过1次,我会收到错误:
Fatal error: Cannot redeclare bh_sort_profiles() (previously declared in
/Users/brturpin/Sites/plugin-development/wp-content/plugins/bh-social-
icons/inc/output.php:68) in /Users/brturpin/Sites/plugin-development/
wp-content/plugins/bh-social-icons/inc/output.php on line 78
我理解这是因为我用来对配置文件进行排序的功能现在被调用两次,但不知道怎么做,所以我没有这个错误:
代码:
$bh_social_profile_array = array (
array(
"id" => "facebook",
"url" => $facebook_url,
"class" => "icon-facebook",
"color" => $facebook_color,
"order" => $facebook_order
),
array(
"id" => "twitter",
"url" => $twitter_url,
"class" => "icon-twitter-1",
"color" => $twitter_color,
"order" => $twitter_order
),
array(
"id" => "pinterest",
"url" => $pinterest_url,
"class" => "icon-pinterest",
"color" => $pinterest_color,
"order" => $pinterest_order
),
array(
"id" => "instagram",
"url" => $instagram_url,
"class" => "icon-instagram",
"color" => $instagram_color,
"order" => $instagram_order
),
array(
"id" => "gplus",
"url" => $gplus_url,
"class" => "icon-gplus",
"color" => $gplus_color,
"order" => $gplus_order,
),
array(
"id" => "linkedin",
"url" => $linkedin_url,
"class" => "icon-linkedin",
"color" => $linkedin_color,
"order" => $linkedin_order
),
array(
"id" => "youtube",
"url" => $youtube_url,
"class" => "icon-youtube",
"color" => $youtube_color,
"order" => $youtube_order
),
array(
"id" => "rss",
"url" => $rss_url,
"class" => "icon-rss",
"color" => $rss_color,
"order" => $rss_order
),
);
function bh_sort_profiles($arr) {
$size = count($arr);
for ($i=0; $i<$size; $i++) {
for ($j=0; $j<$size-1-$i; $j++) {
if ($arr[$j+1]["order"] < $arr[$j]["order"]) {
swap($arr, $j, $j+1);
}
}
}
return $arr;
}
function swap(&$arr, $a, $b) {
$tmp = $arr[$a];
$arr[$a] = $arr[$b];
$arr[$b] = $tmp;
}
$bh_sorted_profiles = bh_sort_profiles($bh_social_profile_array);
?>
<div style="text-align: <?php echo $social_align; ?>;">
<?php foreach($bh_sorted_profiles as $i => $profile): ?>
<?php if ($profile["url"] != ""): ?>
<a href="<?php echo $profile["url"]; ?>" class="social-icons <?php echo $profile["class"]; ?>" style="background-color: <?php if ($profile['color'] != '') { echo $profile['color']; } ?>; color: <?php echo $text_color; ?>; font-size: <?php echo $font_size; ?>; border-radius: <?php echo $border_radius; ?>" target="<?php echo $new_window; ?>"></a>
<?php endif; ?>
<?php endforeach; ?>
</div>