纯代码引入Prism高亮,且按需加载,没有代码的文章不输出Prism的css和js。

Function代码

//Prism高亮按需加载,by minirizhi.com
function Prism_Resources($content) {
    $pre = "/(pre|<\/pre>)/i";
    if(preg_match_all($pre, $content, $matches) && is_single()) {
        $content.= '<script type="text/javascript" src="'.site_url().'/content/themes/minirizhi/assets/js/prism.js"></script>
<link rel="stylesheet" id="prismCSS-css" href="'.site_url().'/content/themes/minirizhi/assets/css/prism.css" type="text/css" media="all" />';
    }
    return $content;
}
add_filter( "the_content", "Prism_Resources");
文章目录