当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Streamline these if statements and output them with their values

问题描述:

Is there a way that I can streamline my process when I'm working with theme options on Wordpress?

Currently I use a setup like this when I'm creating a social links menu for example (With the option value being the link URL);

$twitter = of_get_option('twitter');

$facebook = of_get_option('facebook');

$google-plus = of_get_option('google-plus');

if ($twitter){

echo '<li class="twitter"><a href="'.$twitter.'"><i class="fa fa-twitter"></i></a></li>';

}

if ($facebook){

echo '<li class="facebook"><a href="'.$facebook.'"><i class="fa fa-facebook"></i></a></li>';

}

if ($google-plus){

echo '<li class="google-plus"><a href="'.$google-plus.'"><i class="fa fa-google-plus"></i></a></li>';

}

I'm sure there must be an easier, more streamlined, way to go about doing this?

Any help would be greatly appreciated.

网友答案:

I'd wrapp it in a function.

function getOption($op) {
    $link = of_get_option($op);
    if($link)
        echo '<li class="'.$op.'"><a href="'.$link.'"><i class="fa fa-'.$op.'"></i></a></li>';
}

getOption('twitter');
getOption('facebook');
getOption('google-plus');
分享给朋友:
您可能感兴趣的文章:
随机阅读: