我遵循了 Carbon Fields Association 字段的文档,但是当我尝试使用这些值时,我一无所获。我已经在后端创建了该字段,并且在那里一切似乎都运行良好,但在前端我使用carbon_get_post_meta( $id, $name );
where $id
isget_the_ID()
和$name
is my field name related_products
。
谁能告诉我如何利用这个领域,或指出任何可能有帮助的地方?
谢谢你。
我遵循了 Carbon Fields Association 字段的文档,但是当我尝试使用这些值时,我一无所获。我已经在后端创建了该字段,并且在那里一切似乎都运行良好,但在前端我使用carbon_get_post_meta( $id, $name );
where $id
isget_the_ID()
和$name
is my field name related_products
。
谁能告诉我如何利用这个领域,或指出任何可能有帮助的地方?
谢谢你。
在关联字段中,按以下格式获取值。例子:
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,
) );
注意:这只是一个概念。您需要根据您的要求对其进行修改。