You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在functions.php中注册ACF块遇语法错误求助

解决ACF自定义块注册的语法错误问题

核心错误分析

你的代码存在两个关键问题:

  1. if条件语句的语法错误:if (function_exists('acf_register_block_type');) 里的分号是多余的,PHP会判定条件表达式未结束,因此抛出"预期有')'"的报错。
  2. 函数与钩子挂载的逻辑矛盾:你把注册块的函数ceejtheday_register_acf_blocks定义在if块内部,但add_action却放在if块外面。如果ACF未激活,这个函数不会被定义,add_action会触发"未定义函数"的错误。

修正后的完整代码

// Register ACF blocks
function ceejtheday_register_acf_blocks() {
    // 额外安全校验:确认ACF函数存在
    if (!function_exists('acf_register_block_type')) {
        return;
    }

    acf_register_block_type(array(
        'name'              => 'masonry-images',
        'title'             => __('Masonry Images'),
        'description'       => __('Images in a masonry format.'),
        'render_template'   => 'blocks/masonry-images.php',
        'category'          => 'common',
        'icon'              => 'heart',
        'keywords'          => array('block', 'custom', 'images', 'masonry')
    ));

    acf_register_block_type(array(
        'name'              => 'paragraph',
        'title'             => __('Paragraph'),
        'description'       => __('Large paragraph element with title.'),
        'render_template'   => 'blocks/paragraph.php',
        'category'          => 'common',
        'icon'              => 'heart',
        'keywords'          => array('block', 'custom', 'content', 'text', 'paragraph', 'title')
    ));

    acf_register_block_type(array(
        'name'              => 'full-width-image',
        'title'             => __('Full Width Image'),
        'description'       => __('Image that spans the full width of the page.'),
        'render_template'   => 'blocks/full-width-image.php',
        'category'          => 'common',
        'icon'              => 'heart',
        'keywords'          => array('block', 'custom', 'image', 'full width')
    ));

    acf_register_block_type(array(
        'name'              => 'resume',
        'title'             => __('Resume'),
        'description'       => __('Resume block.'),
        'render_template'   => 'blocks/resume.php',
        'category'          => 'common',
        'icon'              => 'heart',
        'keywords'          => array('block', 'custom', 'image', 'content', 'resume')
    ));

    acf_register_block_type(array(
        'name'              => 'hero-image',
        'title'             => __('Hero Image'),
        'description'       => __('Hero image with title'),
        'render_template'   => 'blocks/hero-image.php',
        'category'          => 'common',
        'icon'              => 'heart',
        'keywords'          => array('block', 'custom', 'image', 'hero')
    ));

    acf_register_block_type(array(
        'name'              => 'cards',
        'title'             => __('Cards'),
        'description'       => __('Card block'),
        'render_template'   => 'blocks/cards.php',
        'category'          => 'common',
        'icon'              => 'heart',
        'keywords'          => array('block', 'custom', 'image', 'cards')
    ));

    acf_register_block_type(array(
        'name'              => 'contact',
        'title'             => __('Contact'),
        'description'       => __('Contact form'),
        'render_template'   => 'blocks/contact.php',
        'category'          => 'common',
        'icon'              => 'heart',
        'keywords'          => array('block', 'custom', 'image', 'contact form')
    ));
}

// 仅在ACF函数存在时挂载钩子
if (function_exists('acf_register_block_type')) {
    add_action('acf/init', 'ceejtheday_register_acf_blocks');
}

修改说明

  • 移除了if条件中的多余分号,修复基础语法错误。
  • 将函数定义移到if块外部,同时把add_action放到判断ACF函数存在的if块内部,避免ACF未激活时出现未定义函数的错误。
  • 在函数内部增加二次校验,作为额外安全保障,防止意外情况触发错误。

内容的提问来源于stack exchange,提问作者user25334650

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 03:02:04