【问题标题】:"No block type exists" when trying to create ACF block for Gutenberg尝试为 Gutenberg 创建 ACF 块时出现“不存在块类型”
【发布时间】:2021-04-16 11:37:54
【问题描述】:

我正在尝试初始化一个名为hero 的新块。目前,我正在尝试为该块分配一个 ACF 字段组,但目前在 WordPress 后端看到了这个:

我已经创建了块并注册了块类型,但看不到哪里有问题?

这是我的方法:

/acf-blocks/blocks.php

<?php

function register_acf_block_types() {

    $hero = array(
        'name'              => 'hero',
        'title'             => __('Hero'),
        'description'       => __(''),
        'render_callback'   => 'block_render',
        'category'          => 'formatting',
        'icon'              => 'admin-comments',
        'keywords'          => array( 'hero' ),
    );

}


$blocks = [
  $hero
];

return $blocks;


// Check if function exists, and hook into setup
if( function_exists('acf_register_block_type') ) {
  add_action('acf/init', 'register_acf_block_types');
}


?>

/acf-blocks/render.php

<?php

/**
 *  This is the callback that renders the block.
 */

 function block_render( $block ) {

    // convert name ("acf/testimonial") into path friendly slug ("testimonial")
    $slug = str_replace('acf/', '', $block['name']);

    // include a template part from within the "template-parts/block" folder
    if( file_exists( get_theme_file_path("/template-parts/block/content-{$slug}.php") ) ) {
        include( get_theme_file_path("/template-parts/block/content-{$slug}.php") );
    }
 }

?>

/acf-blocks/functions.php

<?php


function block_acf_init(){
  $blocks = require(__DIR__.'/inc/acf-blocks/blocks.php');

  foreach($blocks as $block) {
    acf_register_block($block);
  }
}


?>

/template-parts/block/hero.php

<?php

/*
* Block Name: hero
*/


?>

这是我的文件夹结构:

theme
   inc
      acf-blocks
         blocks.php
         functions.php
         render.php
   template-parts
      blocks
         hero.php

【问题讨论】:

    标签: php advanced-custom-fields wordpress-gutenberg gutenberg-blocks create-guten-block


    【解决方案1】:

    将 foreach 循环中的 ACF 函数(在 /acf-blocks/functions.php 中)更改为:

    foreach($blocks as $block) {
        acf_register_block_type($block);
    }
    

    【讨论】:

      猜你喜欢
      • 2021-04-23
      • 1970-01-01
      • 1970-01-01
      • 2019-08-28
      • 2020-02-02
      • 1970-01-01
      • 2020-06-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多