dongtang6775 2014-10-13 16:34
浏览 41

简化这些if语句并使用它们的值输出它们

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.

  • 写回答

1条回答 默认 最新

  • duanjiancong4860 2014-10-13 16:57
    关注

    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');
    
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法