wordpress functions.php中重复的HTML

时间:2014-08-25 06:31:15

标签: php html wordpress

我在wordpress网站的functions.php文件中创建了一些短代码,我正在研究如何最好地实现它。

可以从中选择的240个国家的选择列表将是几个不同的短代码,而不是将整个列表放置4或5次我想知道我们是否可以实现它一次并从函数内部进行调用

这就是我们现在所拥有的:

function form_function() {

ob_start(); 
?>
    ...
    <select id="fc" name="fc" title="From Country">
       <option value="United States">United States</option>
       <option value="Afghanistan">Afghanistan</option>
       ...
       <option value="Zimbabwe">Zimbabwe</option>
    </select>
    ...
    <select id="tc" name="tc" title="To Country">
       <option value="United States">United States</option>
       <option value="Afghanistan">Afghanistan</option>
       ...
       <option value="Zimbabwe">Zimbabwe</option>
    </select>
    ...
<?php
      $html = ob_get_contents();
      ob_end_clean();
      return $html;
}

我们希望将其更改为:

function countrylist() {
    <option value="United States">United States</option>
    <option value="Afghanistan">Afghanistan</option>
    ...
    <option value="Zimbabwe">Zimbabwe</option>
 }


function form_function() {

ob_start(); 
?>
    ...
    <select id="fc" name="fc" title="From Country">
      <? countrylist () ?>
    </select>
    ...
    <select id="tc" name="tc" title="To Country">
      <? countrylist () ?>
    </select>
<?php
      $html = ob_get_contents();
      ob_end_clean();
      return $html;
}

有关如何最好地做到这一点的任何想法?提前致谢

1 个答案:

答案 0 :(得分:0)

也许最好将countrylist()功能更改为

function countrylist() 
{
    $countries = array('USA', 'Canada', 'Ukraine');        
    foreach($countries as $country)
    {
        $html .= '<option value="'.$country.'">'.$country.'</option>'
    }        
    return $html;
}

form_function()<? countrylist () ?>替换为<?= countrylist () ?>