使用 ACF 创建的 Gutenberg Block block.json 在页面编辑器中不可见

问题描述 投票:0回答:1

我试着用 ACF 创建一个古腾堡块,就像这里描述的那样(https://www.billerickson.net/building-acf-blocks-with-block-json/#create-a-block-json-file

我的block.json:

{       
    "name"           : "quick-links",
    "title"         : "Quick Links",
    "apiVersion"    : 2,
    "icon"          : "admin-users",
    "mode"          : "auto",
    "acf": {
        "mode": "preview",
        "renderTemplate": "quick-links.php"
    },
    "align"         : "full",
    "keywords"      : [ "Quick-Links", "Links" ]
}

我注册区块的插件:

<?php
/**
 * Plugin Name: Gutenberg Block
 * Description: Blocks to display photo/video galleries, quicklinks, downloads, ... 
 * Version: 1.0
 */
add_action( 'init', 'register_acf_blocks', 5 );
function register_acf_blocks() {

register_block_type( __DIR__ . '/blocks/quick-links/block.json' );
}

quick-links.php 只是输出一个字符串。

但是该块在页面编辑器中不可见。没有 PHP 或 JS 错误。 谁能看出问题?

advanced-custom-fields block wordpress-gutenberg
1个回答
0
投票

您的 ACF 区块注册 可能存在一些潜在问题。在你的 block.json 中,你的 block name 的命名空间丢失了,这可能导致块注册失败。

  1. 使用您独特的块名称更新
    namespace

block.json

{
    "name": "namespace/quick-links",
    ...
} 
  1. 函数名称

    register_acf_blocks()
    可能不是唯一的函数名称。问题可能是您的函数正在覆盖同名的现有函数。更新您的块注册功能名称,使其更加独特。

  2. 如果您的块仍未呈现或加载,请确认您的 block.json 的路径是正确的。将一些调试信息添加到您的块注册函数以查明块是否正在注册:

<?php

function register_acf_blocks_namespace_quick_link() {
    $status = register_block_type( __DIR__ . '/blocks/quick-links/' ); // block.json not needed

    // If register_block_type cannot register the block, it returns false 
    if(!$status){
        error_log('block.json not found');
    }   
}

add_action( 'init', 'register_acf_blocks_namespace_quick_link', 5 );

希望上述一个或多个步骤可以帮助解决您的问题..

© www.soinside.com 2019 - 2024. All rights reserved.