有什么关于如何发布包含 ACF 关系字段的 WordPress 文章的建议吗?
使用 iFrame 是最好的选择吗?
有什么关于如何发布包含 ACF 关系字段的 WordPress 文章的建议吗?
使用 iFrame 是最好的选择吗?
嘿,Richard,
几乎从来都不是
而且在这个情况下也不是。
你能再详细说明一下这里的需求吗?另外,你目前尝试过哪些方法?
正如你帖子的标题所指出的,你可以使用 discourse_publish_format_html 过滤器在帖子发送到 Discourse 之前修改其 HTML。ACF 文档中还有一些关于如何使用 ACF 关系字段的良好示例。这些示例可以与 discourse_publish_format_html 过滤器结合使用。
我有两个自定义文章类型(CPT):Memes 和 People。每个类型都有一个名为 meme_person 的 ACF 关系字段。一个 meme 可以关联多个人,同时该 meme 还有一个评论文本字段。每个人的名称就是 People 文章类型的标题。
我希望在 Discourse 上显示 meme 的图片,接着是评论内容以及关联人员的列表。
这是我目前的尝试,但我尚未测试,因为我的 foreach 行完全是凭猜测写的。
这个格式对吗?
// 当发布文章到 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 );
抱歉我有点迟钝,但我不太确定您是想让我审查您的全部代码,还是仅审查 Discourse 过滤器的使用方式。就格式而言,这是正确的,因为该过滤器期望您修改 $output,并使用模板标签来插值变量。有关更多信息,请参阅此主题:
谢谢。该主题指出:模板可使用以下模板标签:{excerpt}, {blogurl}, {author}, {thumbnail}, {featuredimage}。
初看时,我以为这些是唯一可传递的元素,这也是我最初询问 iFrame 的原因。其他一些讨论帖提到,现在已支持通过文章 ID 搜索其他元字段。(这一点或许应该在“模板自定义”讨论帖中提及)
您对该主题最初的回复确认了可以跨关系字段进行搜索,并询问了我目前已尝试过的方法。
我上传的代码是我目前完成的部分,不知您是否对 foreach 这一行有任何见解,因为这是我在关系字段搜索中唯一感到困惑的地方。如果没有,我会继续在其他地方查找,并在此更新进展。
祝好。
对于任何想要扩展 ACF 关系字段的人,以下是最终生效的方案:我发送给 Discourse 的自定义文章类型(CPT)名为 memes,其中包含一个名为 meme_person 的关系字段(作为对象),该字段链接到另一个名为 people 的自定义文章类型。
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 );