【发布时间】:2021-06-01 08:26:39
【问题描述】:
我正在 wordpress 中制作艺术家作品集网站。我正在为自定义帖子类型艺术品制作单个页面,其中包含使用 ACF 创建的大量元数据字段。我在显示 ACF 转发器字段的内容时遇到了麻烦,其中包含合作者的姓名(如果有的话),他们对链接到他们个人网站的艺术作品做出了贡献(如果他们有的话)。此中继器作为一个元素被克隆到艺术品字段组中。
我已经能够使用下面的代码显示带有和不带有链接的合作者姓名。所以它会显示“共同创作者:Name1 (with href)Name2 (no href)”。但是逗号不会出现在协作者之间。我认为这是因为我没有正确创建数组。
任何帮助将不胜感激。
<?php
while (have_posts()) : the_post();
// lots more code here
$artwork_collaborators = get_field('artwork_collaborators'); // get the value of the acf clone field
if ($artwork_collaborators) : // check if has data ?>
<dd id="single-artwork-collaborators"><?php
_e('Co-creators: ', 'aopa'); // multilanguage label for translation
foreach ($artwork_collaborators as $artwork_collaborator) : // foreach loop to get all collaborators
if ($artwork_collaborator['artwork_collaborator_url']) : // check if there is a url for the collaborator
$array_collaborators = array('<a href="'. $artwork_collaborator['artwork_collaborator_url'] . '" target="_blank">' . $artwork_collaborator['artwork_collaborator_name'] . '</a>'); // create array with URL
else :
$array_collaborators = array($artwork_collaborator['artwork_collaborator_name']); // create array without url
endif;
$collaborator_string = implode(", ", $array_collaborators); // implode the array and add commas
echo $collaborator_string; // echo the collaborators with link or no link separated by commas if there is more than one
endforeach; ?>
</dd>
<?php endif;
// lots more code here
endwhile?>
【问题讨论】:
-
谢谢卢克。我探索了那个链接。
标签: php wordpress advanced-custom-fields custom-wordpress-pages implode