苏醒 苏醒
  • 首页
  • WP主题
  • WP插件
  • WP教程
  • 是日一记
  • 博客
    • 留言
    • 唠叨
    • 盆友圈
    • 栏目推荐

suxing

管理员
每天清晨有多少双眼睛睁开,有多少人的意识苏醒过来,便有多少个世界。
IP归属地: 广东
文章
214
评论
236
suxing
2014-09-24 22:35:23 广东

免插件使用Google Code Prettify实现代码高亮

文章字数:1232
阅读时间: 4 分钟
智能摘要 DeepSeek
最近在苏醒网站进行改版时,尝试了多种代码高亮方案,最终选择了Google Code Prettify,因其简单易用和小体积特点受到青睐。教程中提到,通过在文章中检测pre标签并在网页脚本中插入Prettify.js和Prettify.css文件,结合jQuery插件即可实现代码高亮。教程还提供下载链接,包括百度云和官方下载地址。

最近苏醒的网站改版,在集成代码高亮方面,尝试过好几种办法,又免插件的、有用插件的,其中发现Google Code Prettify来实现代码高亮是最简单和方便的,而且prettify 非常小,使用它来实现代码的高亮显示是个不错的选择。接下来,就给大家介绍下主题集成prettify代码高亮的办法:
[quote]教程[/quote]
1.检测文章中是否插入了pre标签。若插入了代码,就在网页的footer部分插入相应的prettify.js和prettify.css。把以下代码放到主题文件的functions.php文件中。

//正则匹配pre插入相应的prettify.js和css by suxing.me
add_filter('wp_footer','add_prettify');
function add_prettify(){
//定义全局post
global $post;
//正则匹配pre开始标签
preg_match_all('|(<pre)|i', $post->post_content, $matches);
if(is_single() && !empty($matches[0])) {
//如果存在pre标签时,就把以下代码加入到footer之中
?>
<link rel="stylesheet" id="is-load-css" href="<?php bloginfo('template_url'); ?>/js/prettify.css"/>
<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/prettify.js"></script>
<script type="text/javascript">
jQuery('document').ready(function(){
jQuery('.post pre').addClass('prettyprint linenums')
.wrap('<div class="precode clearfix"></div>');
window.prettyPrint && prettyPrint();
});
</script>
<?php
}
}

2.把prettify.js和prettify.css都加载主题目录的JS文件夹里面。

3.最后,在编辑文章的时候,输入<pre class="prettyPrint">代码放这里</pre>就可以使用标签实行代码高亮了。

[quote]下载[/quote]
[download-button href=”https://www.suxing.me/go/google-code-prettify”]百度云下载[/download-button][download-button href=”https://code.google.com/p/google-code-prettify/”]官方下载[/download-button]

  • WordPress 教程
  • 代码高亮
  • 免插件代码高亮
等 人表示很赞
12,404
0

评论

空空如也
suxing
suxing
2016-01-12 0:53:13 广东

推荐 代码编辑器Sublime Text 3使用教程及简体中文汉化包

智能摘要 DeepSeek Sublime Text是一款免费的跨平台代码编辑器,支持汉化并安装插件,适合编写HTML等代码。用户分享了安装方法:下载地址、汉 ...
  • WordPress 教程
  • emmet安装
  • emmet插件
  • Sublime text 3
  • Sublime text 3 汉化
26,548 1
suxing
suxing
2014-07-05 13:10:24 广东

推荐 WordPress评论回复邮件提醒及注册会员邮件获取密码

智能摘要 DeepSeek 当主机不支持WordPress的mail()函数时,可使用购买的主题中的SMTP邮箱设置发送邮件。操作步骤为:进入主题设置,找到S ...
  • WordPress 教程
  • WordPress注册邮件
  • WordPress评论回复
7,091 1
suxing
suxing
2023-09-17 1:30:39 广东

推荐 解决升级后提示:无需升级,您的WordPress 数据库已经是最新的了

智能摘要 DeepSeek 升级WordPress 6.3后,后台提示数据库已最新且无法登录,可能是Memcached缓存导致。检查wp-content文件夹 ...
  • WordPress 教程
2,210 0
suxing
suxing
2021-04-30 14:12:00 广东

推荐 WordPress 建立数据库连接时出错

智能摘要 DeepSeek 数据库连接出错通常发生在WordPress无法访问MySQL数据库时。常见原因包括数据库连接信息错误、MySQL进程停止、数据库表 ...
  • WordPress 教程
2,599 0
suxing
suxing
2020-11-13 21:27:04 广东

推荐 Mac 如何使用 docker 安装宝塔环境

智能摘要 DeepSeek 这篇文章介绍了使用Docker安装宝塔的简单流程: 1. **下载Docker**:访问[https://www.docker.com/products/docker-desktop](https://www ...
  • WordPress 教程
9,022 1

我用的 WordPress 主题

2021 年单栏 WordPress 主题

Copyright © 2012-2025 苏醒. All rights reserved. Designed by nicetheme.
  • 首页
  • 主题
  • 教程
  • 博客
  • 唠叨
  • 留言