wordpress ACF字段调用方法

下面把「常用、不常用但有用」的 ACF 字段调用方式一次整理给你,直接复制即可用。

所有示例都假设字段挂在当前页面/文章上;如果在选项页、用户、分类法上,请看第 5 节「其它对象」。

基本语法(PHP 模板文件里)

复制代码
// 返回值(原始)
$val = get_field('field_name');

// 安全输出
echo esc_html(get_field('field_name'));

// 带默认值
echo esc_html(get_field('field_name') ?: '暂无内容');

常见字段类型速查 | 字段类型 | 直接 echo 会得到 | 常用写法 |

|---|---|---|

| Text / Textarea / Number | 字符串 | echo esc_html(get_field('tx')); |

| Image (返回数组) | 数组 | echo wp_get_attachment_image(get_field('img')'ID', 'medium'); |

| Image (返回 URL) | 字符串 | " /> |

| File | 数组 | 下载 | | Select (单选) | 单个值 | echo esc_html(get_field('color')); |

| Select (多选) | 数组 | echo implode(', ', get_field('colors')); |

| Checkbox | 数组 | 同上 | | True / False | bool | if (get_field('show_banner')): ... endif; |

| Wysiwyg | 已格式化 HTML | echo apply_filters('the_content', get_field('intro')); |

| Date Picker | Ymd | echo DateTime::createFromFormat('Ymd', get_field('date'))->format('Y-m-d'); |

| Color Picker | hex | style="color:;" |

| Repeater | 数组 | 见第 3 节 |

| Flexible Content | 数组 | 见第 4 节 |

| Group | 数组 | echo esc_html(get_field('group')'sub_field'); |

Repeater / 嵌套循环

复制代码
<?php if (have_rows('slides')): ?>
    <div class="swiper-wrapper">
    <?php while (have_rows('slides')): the_row(); ?>
        <div class="swiper-slide">
            <img src="<?php echo esc_url(get_sub_field('slide_img')['sizes']['large']); ?>" />
            <h3><?php echo esc_html(get_sub_field('slide_title')); ?></h3>
        </div>
    <?php endwhile; ?>
    </div>
<?php endif; ?>

子字段用 get_sub_field() / the_sub_field()

支持无限嵌套 repeater。

Flexible Content(模块化布局)

复制代码
<?php if (have_rows('sections')): ?>
    <?php while (have_rows('sections')): the_row();
        get_template_part('flex/' . get_row_layout());   // 把每种布局拆成 flex/layout_name.php
    endwhile; ?>
<?php endif; ?>

flex/text.php 例子:

复制代码
<section class="text-section">
    <div class="container">
        <?php echo apply_filters('the_content', get_sub_field('text')); ?>
    </div>
</section>

读取其它对象上的字段 | 对象 | 写法 |

|---|---|

| 选项页(Options Page) | get_field('field_name', 'option'); |

| 用户 | get_field('field_name', 'user_' . $user_id); |

| 分类法 | get_field('field_name', 'term_' . $term_id); |

| 附件 | get_field('field_name', $attachment_id); |

短代码(让编辑在可视化编辑器里插入)

复制代码
// 在 functions.php 注册
add_shortcode('acf', function($atts) {
    $a = shortcode_atts(['field' => '', 'post' => false], $atts);
    return esc_html(get_field($a['field'], $a['post']));
});

使用:acf field="phone"

如需返回图片:acf field="logo" post="option" 再自己包< img>。

常用辅助函数

复制代码
// 直接输出一张图(已含 alt)
the_post_thumbnail('medium', ['class' => 'rounded']);

// 获取多张图(Gallery 字段)
$gallery = get_field('gallery');
foreach ($gallery as $img) {
    echo wp_get_attachment_image($img['ID'], 'thumbnail');
}

// 清除缓存后的新值
wp_cache_flush();
$fresh = get_field('field_name', false, false);   // 第三参数 false = 不格式化

调试技巧

复制代码
<?php
// 看整个字段数组
echo '<pre>', htmlspecialchars(print_r(get_field('repeater_name'), true)), '</pre>';

// 看所有自定义字段
echo '<pre>', htmlspecialchars(print_r(get_post_meta(get_the_ID()), true)), '</pre>';
?>

性能注意

Repeater/Flexible 字段量大时,建议开 「本地 JSON」 或 「字段缓存」(ACF PRO 自动)。

不要在一个循环里反复 get_field('option'),先用变量存起来。

一句话总结「当前文章」get_field('name');

「选项页」get_field('name', 'option');

「子字段」get_sub_field('name');

「循环」have_rows('name') + while 即可。

以上代码全部实测可用,按场景直接粘贴即可。

原文

https://www.wpniu.com/article/6686.html

相关推荐
李纲明1 天前
WordPress 外贸站只在国内测速或同一页中英混排:先核目标市场与语言,再查主机
wordpress·多语言·测速·devdiy·外贸站·目标市场·主机位置
李纲明2 天前
WordPress 安全插件越装越多仍冲突或权限过宽:先核登录与权限分层,再查重叠项
wordpress·文件权限·devdiy·安全插件·登录防护·分层防护·插件重叠
李纲明11 天前
WordPress 标签页或附件页被大量收录:先核站点地图与 noindex,再查归档
wordpress·标签页·devdiy·站点地图·noindex·附件页·索引控制
xbzb13 天前
用 Secret + ConfigMap + PV/PVC 部署 WordPress:一套能跑 https 的最小组合
http·https·wordpress·configmap·secret·pod
李纲明13 天前
WordPress 广告落地页与站内不一致或支付物流说明乱:先核对齐信息,再查模板
wordpress·devdiy·广告落地页·物流说明·支付说明·信息对齐·页面模板
李纲明13 天前
WordPress 帮助文档放在另一系统几乎搜不到:先核子目录与反向代理,再查支柱页
wordpress·反向代理·内容中心·帮助文档·devdiy·子目录·支柱页
李纲明14 天前
WordPress 已加结构化数据却不出富结果:先核 JSON-LD 与插件来源,再查必填
schema·wordpress·结构化数据·json-ld·devdiy·富结果·seo插件
李纲明15 天前
WordPress 区块编辑器排好后前台缺块或提示异常:先核区块校验与粘贴来源,再查插件
wordpress·devdiy·gutenberg·区块编辑器·区块校验·异常内容·可重用区块
李纲明15 天前
WordPress 迁移后首页能开、内页 404 或图片裂图:先核伪静态与站点地址,再查媒体
wordpress·404·伪静态·媒体库·devdiy·站点迁移·站点地址
李纲明16 天前
WordPress 外贸主题切语言后版式乱或海外很慢:先核翻译插件适配,再查海外节点与询盘
wordpress·多语言·询盘·devdiy·主题选型·外贸站·wpml