WordPress代码高亮插件我一直在使用WP Code Highlight,今日我介绍个集成Google Code Prettify实现WordPress代码高亮的方法,我的博客已经换成了这种方法。
直接引用cdn:https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js,我是下载到主题中,所以手动引用:
wp_enqueue_script('prettify-js', get_template_directory_uri() .'/js/run_prettify.js',false,'1.0',true);
我的文章class是.post-content,所以查找.post-content中的pre加class prettyprint。
$(".post-content pre").addClass('prettyprint');
在body标签内增加
onload="PR.prettyPrint()"
这里我是加载文章也和页面中,你也可以全局加载根据需求,加载js需要在jquery之后。prettify不支持在pre标签中影响全局或者出现夸张的标签,比如script、php、body等。