{"id":4971,"date":"2026-01-21T17:50:06","date_gmt":"2026-01-21T09:50:06","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/wpcms\/bjjc\/4971.html"},"modified":"2026-01-21T17:50:07","modified_gmt":"2026-01-21T09:50:07","slug":"wordpress%e7%bd%91%e7%ab%99%e5%8d%95%e7%8b%ac%e9%a1%b5%e9%9d%a2%e7%9a%84%e8%87%aa%e5%ae%9a%e4%b9%89css%e6%a0%b7%e5%bc%8f%e6%b7%bb%e5%8a%a0%e5%88%b0head%e6%a0%87%e7%ad%be%e4%b8%ad%e7%9a%84","status":"publish","type":"bjjc","link":"https:\/\/www.zhaozhao123.cn\/wpcms\/bjjc\/4971.html","title":{"rendered":"WordPress\u7f51\u7ad9\u5355\u72ec\u9875\u9762\u7684\u81ea\u5b9a\u4e49css\u6837\u5f0f\u6dfb\u52a0\u5230&lt;head>\u6807\u7b7e\u4e2d\u7684\u65b9\u6cd5\u53ca\u8be6\u7ec6\u4ee3\u7801"},"content":{"rendered":"<p>WordPress\u7f51\u7ad9\u5355\u72ec\u9875\u9762\u7684\u81ea\u5b9a\u4e49css\u6837\u5f0f\u6dfb\u52a0\u5230&lt;head&gt;\u6807\u7b7e\u4e2d\u7684\u65b9\u6cd5\u53ca\u8be6\u7ec6\u4ee3\u7801\uff1a<\/p><p>\u4ee3\u7801\u80cc\u666f\uff1a\u5728head\u6807\u7b7e\u4e2d\u8f93\u51fapost\u6587\u7ae0\u7c7b\u578b\u7684\u81ea\u5b9a\u4e49\u5b57\u6bb5qian_c_gj_style\u4e2d\u7684css\u6837\u5f0f\u5e76\u538b\u7f29\u4e3a\u4e00\u884c<\/p><p>\u8be6\u7ec6\u4ee3\u7801\uff1a<\/p><pre class=\"wp-block-code\"><code>\/**\n * \u5728head\u6807\u7b7e\u4e2d\u8f93\u51fapost\u6587\u7ae0\u7c7b\u578b\u7684\u81ea\u5b9a\u4e49\u5b57\u6bb5qian_c_gj_style\u4e2d\u7684css\u6837\u5f0f\u5e76\u538b\u7f29\u4e3a\u4e00\u884c\n *\/\nfunction add_post_compressed_css_to_head() {\n    \/\/ \u68c0\u67e5\u662f\u5426\u5728post\u6587\u7ae0\u7c7b\u578b\u9875\u9762\n    if (is_singular('post')) {\n        global $post;\n        \n        \/\/ \u83b7\u53d6\u81ea\u5b9a\u4e49\u5b57\u6bb5\u503c\n        $custom_css = get_post_meta($post-&gt;ID, 'qian_c_gj_style', true);\n        \n        \/\/ \u5982\u679c\u5b57\u6bb5\u4e0d\u4e3a\u7a7a\uff0c\u5219\u8f93\u51fa\n        if (!empty($custom_css)) {\n            \/\/ \u79fb\u9664\u53ef\u80fd\u5b58\u5728\u7684\u591a\u4f59&lt;style&gt;\u6807\u7b7e\n            $custom_css = str_replace(array('&lt;style&gt;', '&lt;\/style&gt;'), '', $custom_css);\n\n            \/\/ \u538b\u7f29CSS\u4e3a\u4e00\u884c\n            $compressed_css = compress_css($custom_css);\n                \n            \n            \/\/ \u8f93\u51fa\u538b\u7f29\u540e\u7684\u6837\u5f0f\n            \n            echo '&lt;style id=\"post-custom-style\"&gt;' . $compressed_css . '&lt;\/style&gt;' . \"n\";\n        }\n    }\n}\nadd_action('wp_head', 'add_post_compressed_css_to_head', 10);\n\n\/**\n * CSS\u538b\u7f29\u51fd\u6570 - \u57fa\u672c\u538b\u7f29\n * \n * @param string $css CSS\u4ee3\u7801\n * @return string \u538b\u7f29\u540e\u7684CSS\u4ee3\u7801\n *\/\nfunction compress_css($css) {\n    \/\/ 1. \u79fb\u9664\u6ce8\u91ca\uff08\/* ... *\/\uff09\n    $css = preg_replace('!\/*[^*]**+([^\/][^*]**+)*\/!', '', $css);\n    \n    \/\/ 2. \u79fb\u9664\u7a7a\u767d\u5b57\u7b26\uff1a\u6362\u884c\u7b26\u3001\u5236\u8868\u7b26\u3001\u591a\u4f59\u7a7a\u683c\n    $css = str_replace(array(\"rn\", \"r\", \"n\", \"t\", '  '), ' ', $css);\n    \n    \/\/ 3. \u79fb\u9664CSS\u89c4\u5219\u5757\u5468\u56f4\u7684\u591a\u4f59\u7a7a\u683c\n    $css = preg_replace('\/s*{s*\/', '{', $css);\n    $css = preg_replace('\/s*}s*\/', '}', $css);\n    $css = preg_replace('\/s*;s*\/', ';', $css);\n    $css = preg_replace('\/s*:s*\/', ':', $css);\n    $css = preg_replace('\/s*,s*\/', ',', $css);\n    \n    \/\/ 4. \u79fb\u9664\u6700\u540e\u4e00\u4e2a\u5206\u53f7\u540e\u7684\u7a7a\u683c\n    $css = preg_replace('\/;}\/', '}', $css);\n    \n    \/\/ 5. \u79fb\u9664\u6240\u6709\u591a\u4f59\u7684\u7a7a\u683c\uff08\u8fde\u7eed\u591a\u4e2a\u7a7a\u683c\u66ff\u6362\u4e3a\u4e00\u4e2a\uff09\n    $css = preg_replace('\/s+\/', ' ', $css);\n    \n    \/\/ 6. \u79fb\u9664\u5f00\u59cb\u548c\u7ed3\u675f\u7684\u7a7a\u683c\n    $css = trim($css);\n    \n    return $css;\n}<\/code><\/pre>","protected":false},"excerpt":{"rendered":"<p>WordPress\u7f51\u7ad9\u5355\u72ec\u9875\u9762\u7684\u81ea\u5b9a\u4e49css\u6837\u5f0f\u6dfb\u52a0\u5230&lt;head&gt;\u6807\u7b7e\u4e2d\u7684\u65b9\u6cd5\u53ca\u8be6\u7ec6\u4ee3\u7801\uff1a \u4ee3\u7801\u80cc\u666f\uff1a\u5728head\u6807\u7b7e\u4e2d\u8f93\u51fapost\u6587\u7ae0\u7c7b\u578b\u7684\u81ea\u5b9a\u4e49\u5b57\u6bb5qian_c_gj_style\u4e2d\u7684css\u6837\u5f0f\u5e76\u538b\u7f29\u4e3a\u4e00\u884c \u8be6\u7ec6\u4ee3\u7801\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":true},"tags":[59],"bjjc2nav":[23],"tuisongtax":[],"class_list":["post-4971","bjjc","type-bjjc","status-publish","hentry","tag-pzjc","bjjc2nav-kuaizhuti"],"acf":{"qian_art_seotitle":"","qian_art_seotitle_source":{"label":"SEO\u6807\u9898","type":"text","formatted_value":""},"qian_art_seokws":"","qian_art_seokws_source":{"label":"SEO\u5173\u952e\u8bcd","type":"text","formatted_value":""},"qian_art_stzhong":"\u5355\u9875CSS\u6837\u5f0f\u6dfb\u52a0\u5230<head>\u4e2d","qian_art_stzhong_source":{"label":"\u4e2d | \u77ed\u6807\u9898","type":"text","formatted_value":"\u5355\u9875CSS\u6837\u5f0f\u6dfb\u52a0\u5230<head>\u4e2d"}},"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/bjjc\/4971","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/bjjc"}],"about":[{"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/types\/bjjc"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/users\/1"}],"wp:attachment":[{"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/media?parent=4971"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/tags?post=4971"},{"taxonomy":"bjjc2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/bjjc2nav?post=4971"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/tuisongtax?post=4971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}