ACF 字段未显示

ACF Field not showing up

我在图像元数据中添加了一些自定义字段并隐藏了原始元数据字段(图像、标题、替代文本)。所以我的新自定义字段是标题、描述、尺寸。

我在ACF之前的模板是这样的:

<?php if ( is_single() ) : ?>
    <h1><?php the_title(); ?></h1>
    <?php else : ?>
    <h1>
        <a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
    </h1>
    <?php endif; // is_single() ?>

    <h3><?php ffm_artists(); ?></h3>
    <h3><?php date_range(); ?></h3>

    <?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>
    <div class="entry-thumbnail">
            <a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_post_thumbnail( "large" ); ?></a>
    </div>
    <?php endif; 

    the_content('Read more...'); 
    $images = get_field('images'); if( $images ): 
        foreach( $images as $image ): ?>
        <figure>
            <a href="<?php echo $image['url']; ?>"><img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" /></a>
            <figcaption>
                <?php if($image['title'] !== '') { echo $image['title'], '<br />'; }?>
                <?php if($image['caption'] !== '') { echo $image['caption'], '<br />'; }?>
                <?php if($image['description'] !== '') { echo $image['description']; }?>
            </figcaption>
        </figure>
    <?php endforeach; ?> 
<?php endif; ?>

添加 ACF 字段后,我将 title、desc 和 dim 字段更改为:

foreach( $images as $image ): ?>
<figure>
<a href="<?php echo $image['url']; ?>"><img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" /></a>
    <figcaption>
        <p><?php the_field('title'); ?></p>
        <p><?php the_field('description'); ?></p>
        <p><?php the_field('dimensions'); ?></p>
    </figcaption>
</figure>
<?php endforeach; ?> 
<?php endif; ?>

现在我的代码只输出空白 <p><\p> 我知道标题、描述和维度字段中有文本。有什么想法吗?

默认情况下,ACF 函数 the_field()get_field() return 自定义字段直接来自当前 post。

要使用它们从其他地方获取字段,例如分类术语、选项页面或 - 在您的情况下 - 媒体附件,您需要通过发送额外的参数。

This page in the ACF docs 说明如何操作。

您需要通过媒体附件的 ID post 作为附加参数发送。所以,你会在你的上下文中看到这样的东西:

<p><?php the_field('title', $image['id']); ?></p>
<p><?php the_field('description', $image['id']); ?></p>
<p><?php the_field('dimensions', $image['id']); ?></p>