【问题标题】:Wordpress select metabox: Set an ID/CLASS for each optionWordpress 选择元框:为每个选项设置一个 ID/CLASS
【发布时间】:2013-09-20 00:58:50
【问题描述】:

我在 Wordpress 上的 functions.php 中创建了一个选择元框。

'fields' => array(
    array(
        'name' => 'CMS Logo',
        'id' => 'cms_icon_image',
        'type' => 'select',
        'options' => array(
            'Wordpress'   => 'Wordpress',
            'Magento'     => 'Magento',
            'OpenCart'    => 'OpenCart',
        )
    )               
)

我需要为每个选择选项设置一个唯一的 ID 或类别。这将有助于设置每个选项的样式(我将为每个选项添加一个带有 css 的图像)

【问题讨论】:

  • 您可以使用 options 数组中的键或值来定义类
  • @Lodder 你能不能给我一些细节,因为我不明白
  • 我只是用你的代码准备了一个例子,完成后会发布它

标签: php wordpress content-management-system


【解决方案1】:

好的,这是您要找的东西:

<?php
$array = array(
        'name' => 'CMS Logo',
        'id' => 'cms_icon_image',
        'type' => 'select',
        'options' => array(
            'Wordpress'   => 'Wordpress',
            'Magento'     => 'Magento',
            'OpenCart'    => 'OpenCart',
        )
    ) ;

foreach ($array as $key => $value) {
    $array[] = $key;

    if (is_array($array[$key])) {
        $results = array_keys($array[$key]);
        echo "<select>";
        foreach ($results as $result => $val){
            echo "<option class='" . $val . "'>" . $val . "</option>";
        }
        echo "</select>";
    }
}
?>

这将为数组中定义的每个选项输出一个带有&lt;option&gt; 的选择框。

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    • 1970-01-01
    • 2017-09-30
    • 1970-01-01
    • 1970-01-01
    • 2016-05-06
    • 2015-12-25
    相关资源
    最近更新 更多