简体   繁体   English

output 与ACF关系相关帖子的字段

[英]output the_field of related posts with ACF Relationship

and first off all, thanks for help.首先,感谢您的帮助。

I have to following code to display related posts from one custom post type to another with ACF Relationships.我必须按照代码显示从一种自定义帖子类型到另一种具有 ACF 关系的相关帖子。

what i want to know, is it possible and how can i rewrite the code, to output any custom field of the related post that i have selected with the relationship field?我想知道,是否有可能以及如何将代码重写为 output 我用关系字段选择的相关帖子的任何自定义字段?

<?php 
$posts = get_field('product_id');

    if( $posts ): ?>
        <ul>
        <?php foreach( $posts as $p ): ?>
            <li>
                <a href="<?php echo get_permalink( $p->ID ); ?>"><?php echo get_the_title( $p->ID ); ?></a>
            </li>
        <?php endforeach; ?>
        </ul>
  <?php endif; ?>

like i do here, is:就像我在这里做的那样,是:

echo get_permalink( $p->ID );

i want to echo:我想回应:

the_field('field_name')

regards, Axel问候,阿克塞尔

If you check the documentation of the the_field() function you'll notice that it can take the post/page ID as the second parameter so you can retrieve the field value of a specific post/page field:如果您查看the_field() function 的文档,您会注意到它可以将帖子/页面 ID 作为第二个参数,因此您可以检索特定帖子/页面字段的字段值:

Parameters参数

the_field($selector, [$post_id], [$format_value]);

  • $selector (string) (Required) The field name or field key. $selector (字符串)(必需)字段名称或字段键。
  • $post_id (mixed) (Optional) The post ID where the value is saved. $post_id (mixed) (可选)保存值的帖子 ID。 Defaults to the current post.默认为当前帖子。
  • $format_value (bool) (Optional) Whether to apply formatting logic. $format_value (bool) (可选) 是否应用格式化逻辑。 Defaults to true.默认为真。

So, for example:因此,例如:

<?php
$posts = get_field('product_id');

if( $posts ): ?>
    <ul>
    <?php foreach( $posts as $p ): ?>
        <li>
            <a href="<?php echo get_permalink( $p->ID ); ?>"><?php echo get_the_title( $p->ID ); ?></a>

            <?php the_field('field_name', $p->ID); ?>
        </li>
    <?php endforeach; ?>
    </ul>
<?php endif; ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM