【问题标题】:How to get a foreach statement output in an array with if statement to implode with commas in Wordpress + ACF?如何使用 if 语句在数组中获取 foreach 语句输出,以在 Wordpress + ACF 中用逗号爆破?
【发布时间】: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


【解决方案1】:

我最终从一位程序员朋友那里得到了一些关于这个问题的帮助。现在输出是“共同创作者:Name1(带 href),Name2(无 href)”,逗号在正确的位置,链接在需要时就在适当的位置,而不是在不存在的地方。

其中一个主要问题是内爆和回声在循环内。这导致他们重复了太多次。

我们还在顶部创建了一个空数组,然后将所有字符串添加到其中。请参阅下面代码中的 cmets:

  <dd id="single-artwork-collaborators"><?php 
    _e('Co-creators: ', 'aopa'); // multilanguage label for translation
    $collaborators_all = []; // Create an empty Array
    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 any of the collaborators
        $collaborator = '<a href="'. $artwork_collaborator['artwork_collaborator_url'] . '" target="_blank">' . $artwork_collaborator['artwork_collaborator_name']  . '</a>'; // add rows in loop to the variable
      else : // check if there are any collaborators without URLS
        $collaborator = $artwork_collaborator['artwork_collaborator_name']; // add these rows in loop to the variable
      endif;
      $collaborators_all[] = $collaborator; // combine in the array created above all the rows of collaborator names, with and without and links, if there are any of each     
    endforeach;        
    $collaborator_string = implode(", ", $collaborators_all); // implode the array and add commas          
    echo $collaborator_string; // echo the collaborators separated by commas if there is more than one ?>
  </dd><?php 
  endif; ?>

【讨论】:

    猜你喜欢
    • 2017-01-06
    • 1970-01-01
    • 2016-06-28
    • 2018-10-07
    • 2018-04-24
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    • 2017-08-30
    相关资源
    最近更新 更多