news 2026/2/22 19:44:37

wordpress ACF字段调用方法

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/2/21 12:13:06

macOS外接显示器控制工具MonitorControl完整使用指南

macOS外接显示器控制工具MonitorControl完整使用指南 【免费下载链接】MonitorControl MonitorControl/MonitorControl: MonitorControl 是一款开源的Mac应用程序&#xff0c;允许用户直接控制外部显示器的亮度、对比度和其他设置&#xff0c;而无需依赖原厂提供的软件。 项目…

作者头像 李华
网站建设 2026/2/16 23:02:21

TemporalKit终极指南:一键解决Stable Diffusion视频抖动问题

TemporalKit终极指南&#xff1a;一键解决Stable Diffusion视频抖动问题 【免费下载链接】TemporalKit An all in one solution for adding Temporal Stability to a Stable Diffusion Render via an automatic1111 extension 项目地址: https://gitcode.com/gh_mirrors/te/T…

作者头像 李华
网站建设 2026/2/18 8:03:12

Path of Building终极指南:精准角色构建与技能计算完整解决方案

Path of Building终极指南&#xff1a;精准角色构建与技能计算完整解决方案 【免费下载链接】PathOfBuilding Offline build planner for Path of Exile. 项目地址: https://gitcode.com/GitHub_Trending/pa/PathOfBuilding 你是否曾经在《流放之路》中投入大量时间打造…

作者头像 李华
网站建设 2026/2/22 3:38:27

Mi-Create:重新定义你的智能手表个性化体验

Mi-Create&#xff1a;重新定义你的智能手表个性化体验 【免费下载链接】Mi-Create Unofficial watchface creator for Xiaomi wearables ~2021 and above 项目地址: https://gitcode.com/gh_mirrors/mi/Mi-Create 你是否曾经看着手腕上那些千篇一律的默认表盘&#xff…

作者头像 李华
网站建设 2026/2/21 5:34:24

Potree技术指南:从零开始掌握WebGL点云可视化

在当今三维数据处理领域&#xff0c;Potree作为一款基于WebGL技术的开源点云渲染器&#xff0c;已经成为处理大规模点云数据的首选工具。无论您是地理信息工程师、建筑设计师还是数字化保护工作者&#xff0c;这款工具都能帮助您在浏览器中高效展示数十亿级别的点云数据&#x…

作者头像 李华
网站建设 2026/2/22 7:09:26

AI音频字幕神器:一键自动生成多语言字幕的终极解决方案

AI音频字幕神器&#xff1a;一键自动生成多语言字幕的终极解决方案 【免费下载链接】openlrc Transcribe and translate voice into LRC file using Whisper and LLMs (GPT, Claude, et,al). 使用whisper和LLM(GPT&#xff0c;Claude等)来转录、翻译你的音频为字幕文件。 项目…

作者头像 李华