【问题标题】:advanced custom fields: update/create image field高级自定义字段:更新/创建图像字段
【发布时间】:2016-01-09 01:50:11
【问题描述】:

我有以下问题:我为我想要存储数百张图像的自定义帖子类型创建了一个带有 acf 的图像字段。

  1. 我用 wordpress 导入这些图片。
  2. 我使用“非常简单的 csv 导入器”运行导入以创建大量帖子并将我在 1. 步骤中上传的图像分配给 acf 图像字段。

我已经尝试了很多 update_field()get_field_object() 函数,但没有任何效果。 update_field() 函数的文档很遗憾很不完整,我必须如何使用这个函数来更新图像字段?

这是我正在使用的代码:

function really_simple_csv_importer_save_meta_filter( $meta, $post, $is_update ) {
  $attachment_url = $meta['field_55f2be2a39177'];
  $image_id = pn_get_attachment_id_from_url( $attachment_url ); // function to retrieve the image id that i need for the update_field()
  $field_key = "field_55f2be2a39177";
  update_field($field_key, $image_id);
  return $meta;
} 

add_filter( 'really_simple_csv_importer_save_meta', 'really_simple_csv_importer_save_meta_filter', 10, 3 );

【问题讨论】:

  • 向我们展示您尝试过的代码以及您失败的地方
  • function really_simple_csv_importer_save_meta_filter( $meta, $post, $is_update ) { $attachment_url = $meta['field_55f2be2a39177']; $image_id = pn_get_attachment_id_from_url( $attachment_url ); // function to retrieve the image id that i need for the update_field() $field_key = "field_55f2be2a39177"; update_field($field_key, $image_id); return $meta;} add_filter( 'really_simple_csv_importer_save_meta', 'really_simple_csv_importer_save_meta_filter', 10, 3 );

标签: image field advanced-custom-fields


【解决方案1】:

在这种情况下,我认为您不需要调用 update_field() 而是可以这样做。

function really_simple_csv_importer_save_meta_filter( $meta, $post, $is_update ) {
  $image_id = pn_get_attachment_id_from_url( $attachment_url ); // function to retrieve the image id that i need for the update_field()
  //set the value of the ACF field to the image id.
  $meta['field_55f2be2a39177'] = $image_id;
  return $meta;
} 

add_filter( 'really_simple_csv_importer_save_meta', 'really_simple_csv_importer_save_meta_filter', 10, 3 );

【讨论】:

    猜你喜欢
    • 2019-11-07
    • 2015-07-07
    • 2014-03-23
    • 2017-01-14
    • 2018-04-15
    • 1970-01-01
    • 2015-08-24
    • 2013-10-06
    • 1970-01-01
    相关资源
    最近更新 更多