0

我昨天开始使用 ACF。我想创建一个新的字段类型。我按照官方网站的说明进行操作。但我不明白一些事情。我希望我的字段包含两个文本区域。我真的不知道该怎么做。所以我创建了一个新字段,它出现在编辑页面中,但我无法处理这两个值。这是我的 create_field 函数在我的字段的 php 文件中的样子(我认为这就是我需要更改的内容):

function create_field( $field ){ ?>
    <table>
      <tr> <th scope="col">Year</th> <th scope="col">Text</th> </tr>
      <tr>
         <td>
          <?php echo '<textarea id="' . $field['id'] . 'year"
              class="' . $field['class'] . '"
              name="' . $field['name'] . 'year" >'
              . $field['value']['year'] .
            '</textarea>';
          ?>
        </td>
        <td>
          <?php echo '<textarea id="' . $field['id'] . 'text"
            class="' . $field['class'] . '"
            name="' . $field['name'] . 'text" >'
              . $field['value']['text'] .
            '</textarea>';
          ?>
        </td>
      </tr>
    </table>
    <?php
}

现在,ACF 总是保存第二个文本区域的值。我不明白的是,ACF 是如何知道保存它的?我怎样才能保存第一个的价值呢?

如果你能在这个领域向我推荐任何东西,我也对教程感兴趣。我试图在互联网上找到更多的字段类型(比内置的更多),但我什么也没找到。

感谢您的帮助:科利

4

1 回答 1

0

我不知道 ACF 是如何工作的,但是您的代码应该将yearand定义text为数组的新级别。

好的,您的代码看起来不错,但您唯一必须更改的是名称:

function create_field( $field ){ ?>
    <table>
      <tr> <th scope="col">Year</th> <th scope="col">Text</th> </tr>
      <tr>
         <td>
          <?php echo '<textarea id="' . $field['id'] . 'year"
              class="' . $field['class'] . '"
              name="' . $field['name'] . '[year]" >'
              . $field['value']['year'] .
            '</textarea>';
          ?>
        </td>
        <td>
          <?php echo '<textarea id="' . $field['id'] . 'text"
            class="' . $field['class'] . '"
            name="' . $field['name'] . '[text]" >'
              . $field['value']['text'] .
            '</textarea>';
          ?>
        </td>
      </tr>
    </table>
    <?php
}

现在,我已将名称从 this 更改$field['name'] . 'year为 this $field['name'] . '[text]。请注意,这里不需要单引号。

现在这会将数据保存为array. 如果你打印,这是你得到的$field['name']

array(
    'year' => 'your saved val',
    'text' => 'your saved val'
)
于 2013-08-03T19:42:35.520 回答