0

这是我的代码:

<div class="search-menu">
  <div class="btn-group fc">
    <a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
      <?php
        $currencies = explode(',', hana_get_opt('_cc'));
        foreach ($currencies as $currency) {
          if ($currency == get_option('woocommerce_currency')){
            echo $currency;
            break;
          }else{
            echo "Select Currency";
            break;
          }
        }
      ?>
      <span class="caret"></span>
    </a>
    <ul class="dropdown-menu currency_switcher">
      <?php
        foreach ($currencies as $currency) {
          if ($currency == get_option('woocommerce_currency'))
            echo '<li><a href="#" class="reset default" data-currencycode="' . $currency . '">' . $currency . '</a></li>';
          else
            echo '<li><a href="#" data-currencycode="' . $currency . '">' . $currency . '</a></li>';
        }
      ?>
    </ul>
  </div>
</div>

效果很好,它会返回我的货币列表并相应地更新页面,我只是质疑用户是否尚未设置值我希望它说要么选择货币,要么只应用我hana_get_opt('_cc')数组中的第一个选项作为默认。这是html生成的代码:

<div class="btn-group fc">
                        <a class="btn dropdown-toggle" data-toggle="dropdown" href="#">undefined<span class="caret"></span></a>
                        <ul class="dropdown-menu currency_switcher">
  <li><a href="#" class="reset default active" data-currencycode="SEK">SEK</a></li>
  <li><a href="#" data-currencycode="EUR">EUR</a></li>                                
                        </ul>
                    </div>

我不是 php 编码器,非常感谢克里斯提供的任何帮助

4

2 回答 2

1
   if(get_option('woocommerce_currency')==' ')
    {
        $currencies = explode(',', hana_get_opt('_cc'));
          foreach ($currencies as $currency) 
                {

                 if $currency == get_option('woocommerce_currency'){
                     {
                     echo $currency;
                     break;
                       }
                              else{
                                        echo "Select Currency";
                                        break;
                                    }
                 }
    }

    else
    {   
      echo "Select Currency";
       $currencies = explode(',', hana_get_opt('_cc'));
     $currency=$currencies [0];
    }
于 2013-04-25T10:38:13.573 回答
0

基于如果没有选择货币get_option('woocommerce_currency')将返回的假设null

<div class="search-menu">
<div class="btn-group fc">
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
  <?php
    $currencies = explode(',', hana_get_opt('_cc'));
    $currentCurrency = get_option('woocommerce_currency')
    if ($currentCurrency === false) {
        echo "select currency";
    }
    else{
        echo $currentCurrency;
    }
  ?>
  <span class="caret"></span>
</a>
<ul class="dropdown-menu currency_switcher">
  <?php
    foreach ($currencies as $currency) {
      if ($currency == $currentCurrency)
        echo '<li><a href="#" class="reset default" data-currencycode="' . $currency . '">' . $currency . '</a></li>';
      else
        echo '<li><a href="#" data-currencycode="' . $currency . '">' . $currency . '</a></li>';
    }
  ?>
</ul>

if ( $currentCurrency != null )用任何适用的内容替换 if 子句,如果get_option('woocommerce_currency')返回任何其他内容,就null好像尚未设置

于 2013-04-30T06:22:24.123 回答