【问题标题】:Advanced Custom Fields the_sub_field()['url'] but it doesn't return the URL高级自定义字段 the_sub_field()['url'] 但它不返回 URL
【发布时间】:2021-10-05 06:44:07
【问题描述】:

这是对这个问题的跟进advanced custom fields can't do nested loops repeater inside repeater have_rows() not doing anything

现在我遇到了无法获取子字段 URL 的问题。

          <div class="tc__agenda-speaker">
              <?php while (have_rows('agenda_event_speakers')) : the_row(); ?>
              <div class="tc__agenda-speaker-headshot">
                  <!-- DEBUG LINE -->
                  <div style="color: red;"><?php echo the_sub_field('agenda_event_speaker_headshot')['src'] ?></div>
                  <img src="<?php the_sub_field('agenda_event_speaker_headshot')['url'] ?>" alt="<?php the_sub_field('agenda_event_speaker_headshot')['alt'] ?>">
              </div>
              <div class="tc__agenda-speaker-info">
                  <h4><?php the_sub_field('agenda_event_speaker_name') ?></h4>
                  <p><?php the_sub_field('agenda_event_speaker_title') ?></p>
              </div>
              <?php endwhile ?>
          </div>
        <?php endif ?>

这一行

&lt;?php the_sub_field('agenda_event_speaker_headshot')['url'] ?&gt;

它的输出是这样的

16835, 16835, Name color 2, Name-color-2.png, 152744, http://website.com/wp-content/uploads/2021/02/Name-color-2.png, http://website.com/post/post-title-here/Name-color-2/, , 86, , , Name-color-2, inherit, 16799, 2021-02-02 16:45:53, 2021-02-02 16:45:53, 0, image/png, image, png, http://website.com/wp-includes/images/media/default.png, 500, 500, Array

字段返回格式为数组

Advanced Custom Fields get sub field image

您可以使用 get_sub_field('imgcolumn_1')['url']; 获取图片的 URL

作为函数 get_sub_field() 返回一个数组。

此文档https://www.advancedcustomfields.com/resources/the_sub_field/ 表明访问索引没有区别

确实如此

这个函数和echo get_sub_field()本质上是一样的

如果我去

&lt;?php echo the_sub_field('agenda_event_speaker_headshot')['url'] ?&gt;

img 源是unknown

如何在高级自定义字段的子字段中访问图片数组的url索引?

【问题讨论】:

    标签: php arrays wordpress advanced-custom-fields


    【解决方案1】:

    “基本相同”和“相同”是你被绊倒的地方。 the_sub_field()get_sub_field() 不同的地方之一是当您处理无法直接转换为字符串的值时。

    回答您的直接问题;你需要完全按照你在问题中所说的去做,即改变:

    <?php the_sub_field('agenda_event_speaker_headshot')['url']?>
    

    <?php echo get_sub_field('agenda_event_speaker_headshot')['url']?>
    

    原因是the_sub_field() 没有返回一个值,它是echoes 的值,因此没有具有url 属性的数组供您访问。您的原始代码所做的与

    $variable = null;
    $variable['url'];
    

    这不是你想要的。

    你得到unknown的原因是the_sub_field()确实尝试输出一些东西;但是因为这个特定的子字段更复杂,所以它不能按您的预期工作。在该函数内部发生了这样的事情:

    $subfieldValue = array();
    echo $subfieldValue;
    return;
    

    我希望如果有人深入了解the_sub_field(),您会看到基本上是这样的:

    function the_sub_field($key){
        $value = get_sub_field($key);
        echo $value;
        return;
    }
    

    【讨论】:

    • 抱歉&lt;?php echo get_sub_field('agenda_event_speaker_headshot')['url']?&gt; 不起作用;不起作用...
    • 你能做&lt;?php var_dump(get_sub_field('agenda_event_speaker_headshot'))?&gt;并发布结果
    • /Users/kawnah/Sites/www.website.com/wp-content/themes/theme-name/template-name.php:443:boolean false
    • 这意味着该字段未设置在您所在的帖子和/或行上
    • 每一行都是不同的类型是这个问题吗?
    【解决方案2】:

    非常简单 - 我一开始没有在现场设置图像...

    这实际上是正确的代码。

    【讨论】:

      猜你喜欢
      • 2019-02-25
      • 2021-02-24
      • 2017-02-12
      • 2016-03-24
      • 2017-12-16
      • 2013-07-14
      • 1970-01-01
      • 2017-08-28
      • 2014-04-02
      相关资源
      最近更新 更多