【问题标题】:How to restrict custom field to accept duplicate value如何限制自定义字段接受重复值
【发布时间】:2013-12-09 11:20:46
【问题描述】:

我正在使用register_post_type 添加一个输入字段,比如说“brand_name”。 我想拒绝该字段的重复项。

我怎样才能在 WordPress 中做到这一点? 请帮帮我。

这是我的sn-p:

function brand_register_meta_boxes() {
if (!class_exists('RW_Meta_Box'))
        return;
    $prefix = 'post_';

    $meta_boxes[] = array(
       'title' => 'Add Brand',
        'pages' => array('brand'),

        'fields' => array(

            array(
            'name' => __( 'Brand Name', 'rwmb' ),
            'desc' => __( 'Add Brand Name', 'rwmb' ),
            'id'   => "{$prefix}title",
            'type' => 'text',
            'required' => true,

            ), 

        )
    );     
        foreach ($meta_boxes as $meta_box) {
        new RW_Meta_Box($meta_box);
    }

}

【问题讨论】:

  • 据我了解,您是从输入字段添加自定义帖子。您可以做的是通过在 wordpress 数据库中添加键/值对来保存所有自定义帖子,此功能应该完成工作 - add_option('brand_name', 'true'); .然后您可以检查该自定义帖子类型是否已注册

标签: php wordpress


【解决方案1】:

这取决于您在 save_post 挂钩上所做的事情,即自定义字段被保存到数据库的时间。看起来您正在使用 RW Meta Box 类。我没有亲自使用过 RW Meta Box,但基于 https://github.com/rilwis/meta-box/blob/master/ 上的 Github 存储库,您应该可以通过为您的字段定义设置 'multiple' => false 来实现这一点。

function brand_register_meta_boxes() {
    if (!class_exists('RW_Meta_Box'))
        return;
    $prefix = 'post_';

    $meta_boxes[] = array(
        'title' => 'Add Brand',
        'pages' => array('brand'),
        'fields' => array(
            array(
            'name' => __( 'Brand Name', 'rwmb' ),
            'desc' => __( 'Add Brand Name', 'rwmb' ),
            'id'   => "{$prefix}title",
            'type' => 'text',
            'required' => true,
            'multiple' => false
            ), 
        )
    );     
    foreach ($meta_boxes as $meta_box) {
        new RW_Meta_Box($meta_box);
    }
}

【讨论】:

    【解决方案2】:

    http://in2.php.net/function.array-unique

    您可以将该独特功能用于独特的品牌名称

    【讨论】:

      猜你喜欢
      • 2020-01-21
      • 1970-01-01
      • 2017-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-24
      • 2020-06-25
      • 1970-01-01
      相关资源
      最近更新 更多