如何将WordPress Elementor默认section布局从boxed改为full width?
修改Elementor Section默认布局为Full Width的Hook方案
1. 永久修改编辑器中Section的默认设置
通过elementor/editor/settings/global/default_values过滤器,直接更改新建Section的默认布局值,后续创建的Section/Inner Section都会自动使用Full Width:
add_filter( 'elementor/editor/settings/global/default_values', 'custom_elementor_section_default_layout' ); function custom_elementor_section_default_layout( $default_values ) { // 将普通Section默认布局设为full_width $default_values['section']['layout'] = 'full_width'; // 如需同步修改Inner Section的默认布局,添加此行 $default_values['inner_section']['layout'] = 'full_width'; return $default_values; }
将代码添加到主题的functions.php文件或自定义插件中即可生效。
2. 强制修改已有Section的前端渲染布局
如果要让已存在的Section也强制变为Full Width,可使用elementor/frontend/section/before_render钩子,在渲染前覆盖布局设置,还可顺便清除默认内边距:
add_action( 'elementor/frontend/section/before_render', 'force_elementor_section_full_width' ); function force_elementor_section_full_width( $section ) { // 强制设置Section布局为full_width $section->set_settings( 'layout', 'full_width' ); // 清除左右内边距(按需调整数值) $section->set_settings( 'padding', [ 'top' => '0', 'right' => '0', 'bottom' => '0', 'left' => '0', 'unit' => 'px', ] ); }
注意事项
- 操作前建议备份主题文件或使用子主题,避免代码错误导致网站异常。
- 上述钩子在Elementor免费版和Pro版均适用,无需额外付费功能。
- 若仅需针对Inner Section处理,可在第二个函数中添加判断:
if ( $section->get_name() === 'inner_section' ) { ... }
内容的提问来源于stack exchange,提问作者user19540948
相关产品推荐
相关产品推荐

