[英]Retrieve meta field values of ACF flexible content items using get_post_meta - WordPress
感谢您的帮助。 这是我要实现的目标:
我有一个名为“广告系列”的自定义帖子类型,还有一个与活动自定义帖子类型相关的名为“国家”的自定义分类法。 当用户将新国家/地区添加到广告系列时,会生成一个新的广告系列帖子,该帖子是当前广告系列的子级。 我正在复制分配给父广告系列的ACF字段,并复制子帖子中的值,但是使用ACF灵活内容字段却遇到了问题。 这是我的代码的一部分,该代码正在检索父帖子字段并使用该值更新子帖子中新创建的ACF字段。
$action_text = get_post_meta($parent_id, 'action_text', true);
update_field('action_text', $action_text, $post_id);
我尝试使用灵活的内容来执行此操作,但是我知道我需要遍历并查找已创建的内容块。 最好的方法是什么?
// About Fields
$about_fields = get_post_meta($parent_id, 'content');
var_dump($about_fields);
$meta_key = // How to retrieve the flexible content keys
$meta_value_of_flexible_content = get_post_meta($parent_id, $meta_key);
if($about_fields) {
}
为了澄清起见,“内容”是灵活的容器名称。 “ text_and_image”是我创建的灵活内容块之一的示例名称。
再次感谢您提供任何见解。
我尝试使用灵活的内容来执行此操作,但是我知道我需要遍历并查找已创建的内容块。
您可以只使用get_field()
和update_field()
函数来复制任何 ACF字段,包括“灵活内容”字段。
因此,例如,克隆整个 content
字段:
$about_fields = get_field( 'content', $parent_id );
if ( $about_fields ) {
update_field( 'content', $about_fields, $post_id );
}
// How to retrieve the flexible content keys
foreach ( $about_fields as $arr ) {
echo 'Layout: ' . $arr['acf_fc_layout']; // e.g. "Layout: text_and_image"
// The rest of items in `$arr` are the SUB-fields of that specific layout as
// identified by the `$arr['acf_fc_layout']`, which is the layout's name. So
// if you have two SUB-fields named `text1` and `image1` respectively, then
// these items are set: `$arr['text1']` and `$arr['image1']`
}
要克隆所有 ACF字段:
$fields = get_fields( $parent_id );
foreach ( $fields as $name => $value ) {
update_field( $name, $value, $post_id );
}
我将其更改为使用get_field()
函数:
$action_text = get_post_meta($parent_id, 'action_text', true);
所以:
$action_text = get_field('action_text', $parent_id);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.