我正在尝试使用 ServerSideRender 从 PHP 渲染一个块,如下所示:
.js 文件:
/**
* WordPress dependencies
*/
const { __ } = wp.i18n;
const { registerBlockType } = wp.blocks;
const { ServerSideRender } = wp.components;
/**
* Internal dependencies
*/
import icons from './../../utils/icons';
registerBlockType( 'name/blockname', {
title: __( 'blockname' ),
description: __( 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.' ),
keywords: [ __( 'recent post' ) ],
icon: icons.block,
category: 'xyz',
edit: ( props ) => {
const {
className,
attributes,
} = props;
return (
<ServerSideRender
block="name/blockname"
attributes={ attributes }
className={ className }
/>
);
},
save: () => {
return null;
},
} );
.php 文件:
register_block_type( '.../blockname', array(
'attributes' => array(
'className' => array(
'type' => 'string',
),
),
'render_callback' => 'render_block',
) );
function render_block( $attr, $content ) {
return 'txt';
}
使成为:
<div>txt</div>
预期的:
<div class="wp-block-name-blockname">txt</div>
一切似乎都正常工作,但没有呈现具有类名的 div。
有什么建议可以解决这个问题吗?先感谢您。