【问题标题】:Retrieve meta field values of ACF flexible content items using get_post_meta - WordPress使用 get_post_meta 检索 ACF 灵活内容项的元字段值 - WordPress
【发布时间】:2018-12-13 08:01:39
【问题描述】:

提前感谢您的帮助。这是我想要实现的目标:

我有一个名为“广告系列”的自定义帖子类型,还有一个与广告系列自定义帖子类型相关的名为“国家/地区”的自定义分类。当用户向活动添加新国家/地区时,会生成一个新的活动帖子,该帖子是当前活动的子项。我正在复制分配给父广告系列的 ACF 字段并复制子帖子中的值,但是我在使用 ACF 灵活内容字段时遇到了问题。这是我的代码的 sn-p,它正在检索父帖子字段并使用该值更新子帖子中新创建的 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”是我创建的灵活内容块之一的示例名称。

再次感谢您提供任何见解。

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    我尝试过使用灵活的内容来执行此操作,但我知道我需要循环 通过并找到已创建的内容块。

    您可以只使用get_field() and update_field() functions 复制任何 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);
    

    【讨论】:

    • 你是一个巫师,专家,和令人敬畏的人都融为一体。谢谢。
    猜你喜欢
    • 2018-05-31
    • 2016-02-01
    • 2013-12-29
    • 2019-08-08
    • 1970-01-01
    • 2015-12-26
    • 2018-12-06
    • 2016-04-07
    • 2020-07-10
    相关资源
    最近更新 更多