You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress主题theme.json设置contentSize后编辑器宽度自动恢复问题

WordPress 6.4.2区块主题contentSize设置失效修复方案
  • 补全theme.json的布局样式配置
    当前配置仅在settings中定义了contentSize,需在styles区块同步指定布局规则,确保编辑器和前端都能应用:

    {
      "version": 2,
      "settings": {
        "layout": {
          "contentSize": "840px",
          "wideSize": "1200px"
        }
      },
      "styles": {
        "layout": {
          "contentSize": "840px",
          "margin": "0 auto"
        }
      }
    }
    
  • 手动调用CSS变量
    既然editor-styles-wrapper已生成正确的--wp--style--global--content-size变量,可在主题的editor-style.css中强制给区块应用该宽度:

    .editor-styles-wrapper .wp-block {
      max-width: var(--wp--style--global--content-size);
      margin-left: auto;
      margin-right: auto;
    }
    /* 按需保留全宽区块,比如封面 */
    .editor-styles-wrapper .wp-block-cover {
      max-width: 100%;
    }
    
  • 重置区块默认布局继承
    部分区块可能默认脱离全局布局,需在theme.json中指定区块继承全局设置:

    {
      "version": 2,
      "settings": {
        "layout": {
          "contentSize": "840px",
          "wideSize": "1200px"
        },
        "blocks": {
          "core/paragraph": {"layout": {"inherit": true}},
          "core/image": {"layout": {"inherit": true}},
          "core/list": {"layout": {"inherit": true}}
        }
      }
    }
    
  • 清除编辑器缓存
    刷新浏览器页面、清除缓存,或禁用编辑器相关缓存插件,确保新配置生效。

  • 确认主题基础支持
    在functions.php中确保主题开启区块编辑器支持:

    add_action('after_setup_theme', function() {
      add_theme_support('block-editor-styles');
      add_theme_support('editor-styles');
      add_editor_style('editor-style.css');
    });
    

内容的提问来源于stack exchange,提问作者Landon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 15:32:16