wordpress - 如何更改 WordPress 中 Gutenberg 块的默认 HTML 输出?
问题描述
我正在尝试更改 WordPress 5.7 中 Gutenberg 块的默认 HTML 输出。例如,core/Group
内部带有段落的块的默认输出是:
<div class="wp-block-group">
<div class="wp-block-group__inner-container">
<p>Lorem ipsum dolor sit amet.</p>
</div>
</div>
我想输出这样的东西:
<table role="presentation" class="my-own-custom-class">
<tr>
<td>
<p>Lorem ipsum dolor sit amet.</p>
</td>
</tr>
</table>
我试过在render_block_core/group
钩子上使用自定义过滤器。但我似乎只能围绕 WordPress 已经输出的内容添加内容。这是一个例子:
function my_group_block_wrapper( $block_content, $block ) {
$content = '<table role="presentation" class="my-own-custom-class"><tr><td>' . $block_content . '</td></tr></table>';
return $content;
}
add_filter( 'render_block_core/group', 'my_group_block_wrapper', 10, 2 );
这就是我得到的:
<table role="presentation" class="my-own-custom-class">
<tr>
<td>
<div class="wp-block-group">
<div class="wp-block-group__inner-container">
<p>Lorem ipsum dolor sit amet.</p>
</div>
</div>
</td>
</tr>
</table>
如何摆脱 WordPress 生成的 div?
解决方案
您实际上可以通过使用parse_blocks()
.
从内容字符串中解析块。
<?php
if ( ! empty( get_the_content() ) ) { //... check if the content is empty
$blocks = parse_blocks( get_the_content() ); //... retrieve blocks from the content
foreach ( $blocks as $block ) { //... loop through blocks
echo wp_strip_all_tags( render_block( $block ) ); //... strip all html and render blocks
};
};
?>
推荐阅读
- php - Laravel .htaccess 循环问题
- python - 使用python使用scrapy splash同时抓取多个不同的url
- javascript - 如何将两个音频文件合并为一个并在javascript中合并后播放
- javascript - 2020 年使用 Speech Synthesis Javascript API 跨浏览器工作
- python - 在 Django 中导入库时遇到问题
- android - 使用片段的不同方式
- amazon-cloudformation - CloudFormation 组合子 - 导入 - 子
- python - APScheduler 作业消失
- java - jDateChooser 的值恢复到默认值
- linkerd - linkerd:自动注入被忽略