簡體   English   中英

使用get_post_meta檢索ACF靈活內容項的元字段值-WordPress

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM