使用ACF构建页面,如何彻底移除WordPress区块编辑器的entry-content字段?
彻底移除WordPress区块编辑器的entry-content字段(适配ACF页面构建)
如果你用ACF构建页面,需要彻底移除区块编辑器里默认的entry-content主内容区域,以下是几种有效方案(绝非仅用display:none隐藏):
方案1:通过PHP钩子限制允许的区块(精准移除核心内容区块)
这种方法会从区块编辑器的允许列表中移除core/post-content区块,彻底让它不再显示:
add_filter('allowed_block_types_all', 'remove_entry_content_block', 10, 2); function remove_entry_content_block($allowed_blocks, $editor_context) { // 仅对页面类型生效,可根据需求改为自定义文章类型 if (!empty($editor_context->post) && $editor_context->post->post_type === 'page') { $allowed_blocks = array_filter($allowed_blocks, function($block) { return $block !== 'core/post-content'; }); } return $allowed_blocks; }
将上述代码添加到主题的functions.php文件中即可生效,它只会影响页面的区块编辑器,不干扰ACF字段或其他区块的正常使用。
方案2:移除文章类型的编辑器支持(彻底禁用主内容字段)
如果需要从底层禁用主内容字段的存储功能,同时保留区块编辑器界面以使用ACF,可以用这段代码:
add_action('init', 'remove_page_editor_support'); function remove_page_editor_support() { // 移除页面类型的编辑器支持,彻底清除主内容字段 remove_post_type_support('page', 'editor'); // 保留区块编辑器支持,确保ACF字段/区块能正常显示 add_post_type_support('page', 'block-editor'); }
这个方案会让WordPress不再为页面类型生成主内容字段,从根源上解决问题,适合完全依赖ACF构建页面的场景。
方案3:通过JavaScript注销核心内容区块(前端层面彻底移除)
如果需要更灵活的控制(比如仅在特定条件下移除),可以用JS实现:
- 在主题目录下创建
js/remove-entry-content.js文件,写入:
wp.domReady(function() { // 注销核心的文章内容区块 wp.blocks.unregisterBlockType('core/post-content'); });
- 在主题的
functions.php中注册并加载该脚本:
add_action('enqueue_block_editor_assets', 'enqueue_remove_entry_content_script'); function enqueue_remove_entry_content_script() { if (get_post_type() === 'page') { wp_enqueue_script( 'remove-entry-content', get_template_directory_uri() . '/js/remove-entry-content.js', array('wp-blocks', 'wp-dom-ready'), wp_get_theme()->get('Version'), true ); } }
这种方法在编辑器加载时动态注销目标区块,同样能彻底移除主内容区域。
内容的提问来源于stack exchange,提问作者Robert
相关产品推荐
相关产品推荐

