首页 > 解决方案 > Wordpress ACF 显示关系问题

问题描述

我无法找到用于显示两种自定义帖子类型之间关系字段的文档。

本质上,我有一个包含商店名称和图像的商店帖子类型。然后我有一个产品帖子类型,其中每个产品都有字段,您可以在其中选择它在哪些商店可用,以及该商店中产品链接的部分。

设置:我有两种自定义帖子类型 1:商店,2:产品

“产品”的字段是

“商店”的字段是

我能够显示零售商链接的自定义字段类型,但无法将零售商名称和图像拉入页面。

到目前为止我所拥有的

              <?php
        if( have_rows('product_stores') ): ?>
            <?php while( have_rows('product_stores') ): the_row(); ?>

                <?php the_sub_field('store_link'); ?>

            <?php endwhile; ?>
        <?php endif; ?>

标签: phpwordpressrelationshipadvanced-custom-fields

解决方案


您不需要在其中使用带有关系字段的中继器字段 - 只需使用单个关系字段。该字段将返回一个帖子对象数组,您可以从中提取标题和链接。

<?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(); 
}

?>

推荐阅读