# ACF Relationship字段在 discourse\_publish\_format\_html 中

**URL:** https://meta.discourse.org/t/acf-relationship-field-in-discourse-publish-format-html/186961
**Category:** WordPress
**Created:** [2021年四月15日 13:42 UTC](https://meta.discourse.org/t/acf-relationship-field-in-discourse-publish-format-html/186961 "2021-04-15T13:42:46Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![RichardC](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/richardc/32/216860_2.png) [@RichardC](https://meta.discourse.org/u/RichardC)
#### Post date: [2021年四月15日 13:42 UTC](https://meta.discourse.org/t/acf-relationship-field-in-discourse-publish-format-html/186961/1 "2021-04-15T13:42:46Z")

</div>

有什么关于如何发布包含 ACF 关系字段的 WordPress 文章的建议吗？

使用 iFrame 是最好的选择吗？

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [2021年四月15日 22:36 UTC](https://meta.discourse.org/t/acf-relationship-field-in-discourse-publish-format-html/186961/2 "2021-04-15T22:36:22Z")

</div>

嘿，Richard，

> [@RichardC](#):
>
> iFrame 是我最好的选择吗？

几乎从来都不是 😉 而且在这个情况下也不是。

> [@RichardC](#):
>
> 关于如何发布包含 ACF 关系字段的 WordPress 帖子，有什么建议吗？

你能再详细说明一下这里的需求吗？另外，你目前尝试过哪些方法？

正如你帖子的标题所指出的，你可以使用 `discourse_publish_format_html` 过滤器在帖子发送到 Discourse 之前修改其 HTML。ACF 文档中还有一些关于如何使用 ACF 关系字段的良好示例。这些示例可以与 `discourse_publish_format_html` 过滤器结合使用。

> **[ACF | Relationship](https://www.advancedcustomfields.com/resources/relationship/)**
>
> Description The Relationship field provides a dual-column component to select one or more posts, pages or custom post type items, providing search, post

---

<div class="post-metadata">

### Author: ![RichardC](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/richardc/32/216860_2.png) [@RichardC](https://meta.discourse.org/u/RichardC)
#### Post date: [2021年四月16日 02:49 UTC](https://meta.discourse.org/t/acf-relationship-field-in-discourse-publish-format-html/186961/3 "2021-04-16T02:49:29Z")

</div>

我有两个自定义文章类型（CPT）：Memes 和 People。每个类型都有一个名为 meme\_person 的 ACF 关系字段。一个 meme 可以关联多个人，同时该 meme 还有一个评论文本字段。每个人的名称就是 People 文章类型的标题。

我希望在 Discourse 上显示 meme 的图片，接着是评论内容以及关联人员的列表。

这是我目前的尝试，但我尚未测试，因为我的 foreach 行完全是凭猜测写的。

这个格式对吗？

```plaintext
// 当发布文章到 Discourse 时

function rs_custom_publish_format_html( $output, $post_id ) {

	$post = get_post( $post_id );
	$type = get_post_type($post_id);
	
	// 如果存在特色图片，则使用它
	if ( has_post_thumbnail( $post_id ) ) {
		$image = "{thumbnail}<br><br>";
	} else {
		$image = "";
	}

    // 如果文章类型是 meme，则获取自定义字段信息
    if ( 'memes' === $type) {
    	
	$comment = get_post_meta($post->ID, '_comment', true);
	$people = ''
	$meme_person = get_field('meme_person');
	if( $meme_person ):
		foreach( $meme_person as $people ):
			$title = get_the_title( $featured_post->ID );
			$people .= $title ? . ', '
		<?php endforeach; ?>
		</ul>
	<?php endif; ?>

        <?php ob_start(); ?>
  
        <small>Originally published at: {blogurl}</small><br><br>
        <?php echo $image ?>
        <?php echo $comment ?><br>
        <?php echo $people ?>
        {excerpt}
	  
        <?php 
        $output = ob_get_clean();
        
        return $output;
    }
      
    // 否则返回
    ob_start();
    the_permalink( $post_id );
    $output = ob_get_clean();
    return $output;
    
}
add_filter( 'discourse_publish_format_html', 'rs_custom_publish_format_html', 10, 2 );

```

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [2021年四月16日 05:03 UTC](https://meta.discourse.org/t/acf-relationship-field-in-discourse-publish-format-html/186961/4 "2021-04-16T05:03:47Z")

</div>

> [@RichardC](#):
>
> 这是正确的格式吗？

抱歉我有点迟钝，但我不太确定您是想让我审查您的全部代码，还是仅审查 Discourse 过滤器的使用方式。就格式而言，这是正确的，因为该过滤器期望您修改 `$output`，并使用模板标签来插值变量。有关更多信息，请参阅此主题：

> [@Customize the structure of WP Discourse templates](https://meta.discourse.org/t/wp-discourse-template-customization/50754#add-the-featured-image-to-the-publish-template):
>
> The [WP Discourse](https://github.com/discourse/wp-discourse) plugin uses HTML templates to add markup to Discourse comments for publishing on WordPress and to WordPress posts for publishing on Discourse. The text content of these templates can be customized on the plugin’s options page. To change the structure of a templates requires you to add a function to your theme’s functions.php file This #howto will explain how to do that. The templates In the plugin’s code, the templates are static functions that return a mixture of html and templ…

---

<div class="post-metadata">

### Author: ![RichardC](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/richardc/32/216860_2.png) [@RichardC](https://meta.discourse.org/u/RichardC)
#### Post date: [2021年四月16日 12:18 UTC](https://meta.discourse.org/t/acf-relationship-field-in-discourse-publish-format-html/186961/5 "2021-04-16T12:18:34Z")

</div>

谢谢。该主题指出：模板可使用以下模板标签：`{excerpt}, {blogurl}, {author}, {thumbnail}, {featuredimage}`。

初看时，我以为这些是唯一可传递的元素，这也是我最初询问 iFrame 的原因。其他一些讨论帖提到，现在已支持通过文章 ID 搜索其他元字段。（这一点或许应该在“模板自定义”讨论帖中提及）

您对该主题最初的回复确认了可以跨关系字段进行搜索，并询问了我目前已尝试过的方法。

我上传的代码是我目前完成的部分，不知您是否对 `foreach` 这一行有任何见解，因为这是我在关系字段搜索中唯一感到困惑的地方。如果没有，我会继续在其他地方查找，并在此更新进展。

祝好。

---

<div class="post-metadata">

### Author: ![RichardC](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/richardc/32/216860_2.png) [@RichardC](https://meta.discourse.org/u/RichardC)
#### Post date: [2021年四月16日 19:36 UTC](https://meta.discourse.org/t/acf-relationship-field-in-discourse-publish-format-html/186961/6 "2021-04-16T19:36:46Z")

</div>

对于任何想要扩展 ACF 关系字段的人，以下是最终生效的方案：我发送给 Discourse 的自定义文章类型（CPT）名为 `memes`，其中包含一个名为 `meme_person` 的关系字段（作为对象），该字段链接到另一个名为 `people` 的自定义文章类型。

```php
function rs_custom_publish_format_html( $output, $post_id ) {

	$post = get_post( $post_id );
	$type = get_post_type($post_id);
	
	// 如果存在特色图片，则使用它
	if ( has_post_thumbnail( $post_id ) ) {
		$image = "{thumbnail}<br><br>";
	} else {
		$image = "";
	}

    // 如果文章类型是 memes，则获取自定义字段信息
    if ( 'memes' === $post->post_type) {
    	$comment = get_post_meta($post->ID, 'comment', true);
		$peopleout = ' ';

		$persons = get_post_meta($post->ID, 'meme_person', true);
		if( $persons ):
			foreach( $persons as $person ):
		        $title = get_the_title($person);
				$peopleout .= $title . ' ' ;
			endforeach;
		endif; ?>

        <?php ob_start(); ?>
  
        <small>Originally published at: {blogurl}</small><br><br>
        <?php echo $image ?>
        <?php echo $comment ?><br>
        <?php echo $peopleout ?>

        {excerpt}
	  
        <?php 
        $output = ob_get_clean();
        
        return $output;
    }
  
      
    // 否则返回
    ob_start();
    the_permalink( $post_id );
	$output = ob_get_clean();
	return $output;
    
}
add_filter( 'discourse_publish_format_html', 'rs_custom_publish_format_html', 10, 2 );

```

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [2021年五月16日 19:37 UTC](https://meta.discourse.org/t/acf-relationship-field-in-discourse-publish-format-html/186961/7 "2021-05-16T19:37:41Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
