php - Wordpress ACF 显示关系问题
问题描述
我无法找到用于显示两种自定义帖子类型之间关系字段的文档。
本质上,我有一个包含商店名称和图像的商店帖子类型。然后我有一个产品帖子类型,其中每个产品都有字段,您可以在其中选择它在哪些商店可用,以及该商店中产品链接的部分。
设置:我有两种自定义帖子类型 1:商店,2:产品
“产品”的字段是
[中继器] product_stores
[子字段] store_name(关系)
[子字段] store_link (URL)
“商店”的字段是
标题(WP默认标题)
图像(图像)
我能够显示零售商链接的自定义字段类型,但无法将零售商名称和图像拉入页面。
到目前为止我所拥有的
<?php
if( have_rows('product_stores') ): ?>
<?php while( have_rows('product_stores') ): the_row(); ?>
<?php the_sub_field('store_link'); ?>
<?php endwhile; ?>
<?php endif; ?>
解决方案
您不需要在其中使用带有关系字段的中继器字段 - 只需使用单个关系字段。该字段将返回一个帖子对象数组,您可以从中提取标题和链接。
<?php
$stores = get_field('product_stores'); // your Relationship field
if( $stores ) {
foreach( $stores as $post) {
setup_postdata($post);
the_title();
the_permalink(); // pull whatever you need from the post.
}
wp_reset_postdata();
}
?>
推荐阅读
- cxf - BSP:R5417: Any SIG_KEY_INFO MUST contain a SECURITY_TOKEN_REFERENCE child element
- android - 为什么在使用 Firebase Recycler View Adapter 时出现调用目标异常?
- mautic - 使用 Mautic API 向现有联系人添加标签
- java - 将查询结果打印到 jsp 表 Java Spring MVC jsp
- python-asyncio - asyncio 中的非阻塞套接字 connect()
- android - 导航栏颜色不变
- matlab - 使用 5 个组件的 gmdistribution 在 100 次迭代中无法收敛
- node.js - 有没有办法在 Heroku 上安装 GraphicsMagick?
- javascript - 如何计算两个输入字段并在输入元素中显示总和
- python - 理解时间增量的概念