如何使用Carbon Fields 3显示“关联”字段

时间:2019-05-07 02:55:08

标签: php wordpress carbon-fields

我遵循了Carbon Fields Association字段的文档,但是当我尝试使用这些值时,我什么也没得到。我已经在后端创建了该字段,并且在那里一切正常,但是在前端我正在使用carbon_get_post_meta( $id, $name );,其中$idget_the_ID()和{{1} }是我的字段名称$name

任何人都可以告诉我如何利用此领域,或者将我指向可能有帮助的地方吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

在关联字段中,以以下格式获取值。示例:

Array
(
    [0] => Array
        (
            [value] => post:post:11876
            [type] => post
            [subtype] => post
            [id] => 11876
        )
    [1] => Array
        (
            [value] => post:post:12101
            [type] => post
            [subtype] => post
            [id] => 12101
        )
)

现在您有了数据,您可以获取它并进行相应显示。例子。

这将从给定数组中获取ID。

$ids = wp_list_pluck( $related_products, 'id' );

现在,您可以使用这些ID来获取帖子并根据需要显示。

$related_products_details = get_posts( array(
    'include' => $ids,
    ) );

注意:这只是一个概念。您需要根据需要对其进行修改。