WordPress使用ACF时the_title()输出额外内容的问题求助
WordPress中ACF调用
the_title()额外输出内容的排查与解决 问题场景
使用Advanced Custom Fields(ACF)结合PHP代码输出自定义对象标题时,the_title()函数除了输出目标post_title(Visit our website),还额外输出了Loyalty Points。通过var_dump()确认目标对象的post_title正确,但前端输出包含多余内容;循环中多次输出.说明存在多次循环,但多余内容未在当前var_dump结果中体现。
原代码
foreach ($actions as $action ){ ?> <?php var_dump($action); ?> <?php foreach ($action['lp_earning_method'] as $post){ setup_postdata($post); echo the_title(); wp_reset_postdata(); echo "."; } ?>
var_dump结果
array (size=1) 'lp_earning_method' => object(WP_Post)[19675] public 'ID' => int 125527 public 'post_author' => string '1' (length=1) public 'post_date' => string '2022-05-30 14:33:18' (length=19) public 'post_date_gmt' => string '2022-05-30 13:33:18' (length=19) public 'post_content' => string '' (length=0) public 'post_title' => string 'Visit our website' (length=17) public 'post_excerpt' => string '' (length=0) public 'post_status' => string 'publish' (length=7) public 'comment_status' => string 'closed' (length=6) public 'ping_status' => string 'closed' (length=6) public 'post_password' => string '' (length=0) public 'post_name' => string 'dbd_loyalty-lws_woorewards_pro_events_visit' (length=43) public 'to_ping' => string '' (length=0) public 'pinged' => string '' (length=0) public 'post_modified' => string '2022-09-06 16:54:25' (length=19) public 'post_modified_gmt' => string '2022-09-06 15:54:25' (length=19) public 'post_content_filtered' => string '' (length=0) public 'post_parent' => int 125516 public 'guid' => string 'https://adeline.templweb.com/news/lws-wre-event/dbd_loyalty/dbd_loyalty-lws_woorewards_pro_events_visit/' (length=104) public 'menu_order' => int 0 public 'post_type' => string 'lws-wre-event' (length=13) public 'post_mime_type' => string '' (length=0) public 'comment_count' => string '0' (length=1) public 'filter' => string 'raw' (length=3)
前端实际输出
Visit our website.................Loyalty Points.......
排查核心原因
the_title()的钩子特性:the_title()默认直接输出内容,且会触发the_title钩子。你使用的积分插件(LWS WooRewards Pro)可能通过该钩子注入了Loyalty Points内容。- 外层循环未排查全量数据:当前仅
var_dump了一个$action对象,但外层foreach ($actions as $action)可能遍历了多个对象,其中某个对象关联了标题为Loyalty Points的内容。 setup_postdata()的副作用:虽然调用了wp_reset_postdata(),但全局变量的残留可能导致后续输出异常。
解决方案
方案1:直接输出原始post_title(最稳妥)
绕过the_title()钩子,直接调用对象的post_title属性,确保输出数据库原始内容:
foreach ($actions as $action ){ ?> <?php foreach ($action['lp_earning_method'] as $post){ // 直接输出原始标题,添加esc_html避免XSS风险 echo esc_html($post->post_title); echo "."; } ?>
方案2:使用get_the_title()替代the_title()
get_the_title()返回标题字符串而非直接输出,可避免钩子的自动输出影响:
foreach ($actions as $action ){ ?> <?php foreach ($action['lp_earning_method'] as $post){ setup_postdata($post); // 获取标题字符串并输出,同样添加安全转义 echo esc_html(get_the_title()); wp_reset_postdata(); echo "."; } ?>
方案3:排查外层循环的全量数据
确认$actions数组是否包含多余元素,导致循环多次输出:
// 先打印全量$actions数据,确认是否有多余元素 var_dump($actions); foreach ($actions as $index => $action ){ echo "--- 第{$index}个Action ---"; foreach ($action['lp_earning_method'] as $post){ echo esc_html($post->post_title); echo "."; } }
关键提示
- 积分类插件常通过
the_title钩子修改自定义类型文章的标题输出,这是Loyalty Points的主要来源。 - 所有前端输出建议添加
esc_html()进行转义,避免跨站脚本攻击(XSS)。
内容的提问来源于stack exchange,提问作者Naomi S
相关产品推荐
相关产品推荐

