【问题标题】:Wordpress ACF display relationship issueWordpress ACF 显示关系问题
【发布时间】:2019-03-13 13:31:13
【问题描述】:

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

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

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

“产品”的字段是

  • [中继器] product_stores

    • [子字段] store_name (RELATIONSHIP)

    • [子字段] 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 wordpress relationship advanced-custom-fields


【解决方案1】:

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

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

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-03
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    • 2017-06-20
    • 2015-12-05
    • 2016-11-04
    • 2020-10-18
    相关资源
    最近更新 更多