简体   繁体   English

使用get_post_meta检索ACF灵活内容项的元字段值-WordPress

[英]Retrieve meta field values of ACF flexible content items using get_post_meta - WordPress

Thanks for your help in advance. 感谢您的帮助。 Here's what I'm trying to achieve: 这是我要实现的目标:

I have a custom post type called 'Campaigns' and I have a custom taxonomy called 'Countries' that is related to the campaign custom post type. 我有一个名为“广告系列”的自定义帖子类型,还有一个与活动自定义帖子类型相关的名为“国家”的自定义分类法。 When a user adds a new country to a campaign a new campaign post is generated that is the child of the current campaign. 当用户将新国家/地区添加到广告系列时,会生成一个新的广告系列帖子,该帖子是当前广告系列的子级。 I'm duplicating the ACF fields that are assigned to the parent campaign and replicating the values in the child post, however I've run into an issue using the ACF flexible content fields. 我正在复制分配给父广告系列的ACF字段,并复制子帖子中的值,但是使用ACF灵活内容字段却遇到了问题。 Here'a snippet of my code that is retrieving the parent post fields and updating the newly created ACF field in the child post with that value. 这是我的代码的一部分,该代码正在检索父帖子字段并使用该值更新子帖子中新创建的ACF字段。

$action_text = get_post_meta($parent_id, 'action_text', true);
update_field('action_text', $action_text, $post_id);

I've tried doing this with flexible content, but I know I need to loop through and find what content blocks have been created. 我尝试使用灵活的内容来执行此操作,但是我知道我需要遍历并查找已创建的内容块。 What is the best way to go about this? 最好的方法是什么?

// 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) {

        }

For clarification 'content' is the flexible container name. 为了澄清起见,“内容”是灵活的容器名称。 'text_and_image' is an example name of one of the flexible content blocks I've created. “ text_and_image”是我创建的灵活内容块之一的示例名称。

Thanks again for any insights. 再次感谢您提供任何见解。

I've tried doing this with flexible content, but I know I need to loop through and find what content blocks have been created. 我尝试使用灵活的内容来执行此操作,但是我知道我需要遍历并查找已创建的内容块。

You could just use the get_field() and update_field() functions to duplicate any ACF fields, including Flexible Content fields. 您可以只使用get_field()update_field()函数来复制任何 ACF字段,包括“灵活内容”字段。

So for example, to clone the whole content field: 因此,例如,克隆整个 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']`
}

Additional Code 附加代码

To clone all ACF fields: 要克隆所有 ACF字段:

$fields = get_fields( $parent_id );
foreach ( $fields as $name => $value ) {
    update_field( $name, $value, $post_id );
}

Additional Note 附加说明

I'd change this to use the get_field() function: 我将其更改为使用get_field()函数:

$action_text = get_post_meta($parent_id, 'action_text', true);

So: 所以:

$action_text = get_field('action_text', $parent_id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM