假设包含一个核心/库块,我想在其中自己渲染。如何在服务器端渲染中获取库ID或图像ID。php?
块js公司:
const { Gallery, InspectorControls, InnerBlocks } = wp.blockEditor;
const { registerBlockType } = wp.blocks;
const { Button, PanelBody, TextControl } = wp.components;
const ALLOWED_BLOCKS = [\'core/gallery\'];
const MY_TEMPLATE = [
[\'core/gallery\', { placeholder: \'Gallery\' }],
];
registerBlockType(\'my-gallery-block/main\', {
title: \'My Gallery\',
icon: \'dashicons-format-gallery\',
category: \'my-blocks\',
attributes: {
htmlId: {
type: \'string\'
},
galleryIds: {
type: \'string\'
}
},
edit({ attributes, className, setAttributes }) {
return (<div>
<div className="button-container">
Gallery Block
<InnerBlocks
allowedBlocks={ALLOWED_BLOCKS}
template={MY_TEMPLATE}
templateLock="all"
></InnerBlocks>
</div>
<InspectorControls>
<PanelBody
title="Settings"
initialOpen={true}
>
<TextControl
label="HTML ID"
help="(optional)"
value={attributes.htmlId}
onChange={(content) => setAttributes({ htmlId: content })}
/>
</PanelBody>
</InspectorControls>
</div>);
},
save() {
return (<InnerBlocks.Content />);
}
});
提供php:<?php function render_gallery_block( $attributes, $content ) {
// How do I get the gallery ID or the Image IDs within the gallery?
ob_start();
?>
<?php
$output = ob_get_contents(); // collect output
ob_end_clean(); // Turn off ouput buffer
return $output; // Print output
}
我知道$content
变量已将渲染的库块包含为HTML字符串,但我希望尽可能避免从HTML字符串中提取图像URL。有人知道更好的方法吗?