【问题标题】:Advanced Custom Fields Hidden Field高级自定义字段隐藏字段
【发布时间】:2018-08-06 13:08:45
【问题描述】:

我在 Wordpress 站点中使用 ACF 来配置自定义帖子类型的一些选项,并使用 ACF 唯一 ID 插件生成一个字段,该字段为该字段构建唯一 ID。它的配置非常基础:

class acf_field_unique_id extends acf_field {

function __construct() {

    /*
    *  name (string) Single word, no spaces. Underscores allowed
    */

    $this->name = 'unique_id';


    /*
    *  label (string) Multiple words, can include spaces, visible when selecting a field type
    */

    $this->label = __('Unique ID', 'acf-unique_id');


    /*
    *  category (string) basic | content | choice | relational | jquery | layout | CUSTOM GROUP NAME
    */

    $this->category = 'layout';


    /*
    *  l10n (array) Array of strings that are used in JavaScript. This allows JS strings to be translated in PHP and loaded via:
    *  var message = acf._e('unique_id', 'error');
    */

    $this->l10n = array(
    );


    // do not delete!
    parent::__construct();

}


/*
*  render_field()
*
*  Create the HTML interface for your field
*
*  @param   $field (array) the $field being rendered
*
*  @type    action
*  @since   3.6
*  @date    23/01/13
*
*  @param   $field (array) the $field being edited
*  @return  n/a
*/
function render_field( $field ) {
    ?>
    <input type="text" readonly="readonly" name="<?php echo esc_attr($field['name']) ?>" value="<?php echo esc_attr($field['value']) ?>" />
    <?php
}
}

虽然所有这些功能都很好,但 ID 字段被设计为内部 ID 标记,最终用户不需要在 Wordpress 中看到。有没有办法隐藏与该字段关联的列,这样我就不会失去宝贵的屏幕空间?该列占宽度的 33%,并且由于它用于不必要的字段,因此占用了太多空间。我尝试将它隐藏在 CSS 和 Javascript 中,但由于某种原因,这使得结束列(重复行号和 +/- 符号)更大。

【问题讨论】:

    标签: wordpress advanced-custom-fields


    【解决方案1】:

    您是否尝试将其更改为input[type="hidden"]

    function render_field( $field ){
        echo '<input type="hidden" readonly="readonly" name="'. esc_attr( $field['name'] ) .'" value="'. esc_attr( $field['value'] ) .'" />';
    }
    

    【讨论】:

    • 这可能会起作用如果您检查了哪个字段正在使用一些条件逻辑呈现。此函数似乎呈现所有字段,但应该只隐藏其中一个。
    • 我认为它只呈现了一个字段,即readonly 和所有字段。但是,如有必要,用if( $field['name'] === 'desired name' ) 包装它是微不足道的
    • @Xhynk - 是的,表格单元格仍然会被渲染,所以它只是变成了一个很大的空白空间。
    • 听起来您需要以您喜欢的任何方式(type="hidden"display: none; 等)隐藏它,然后为其他行元素应用一些额外的 CSS。但是,如果没有可用的代码,很难给出准确的建议
    猜你喜欢
    • 1970-01-01
    • 2017-12-16
    • 1970-01-01
    • 2016-09-03
    • 1970-01-01
    • 2018-08-26
    • 2013-07-14
    • 2013-11-25
    • 1970-01-01
    相关资源
    最近更新 更多