苏醒 苏醒
  • 首页
  • 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,697
0

评论

空空如也
suxing
suxing
2014-07-05 13:32:45 广东

推荐 简单几步让你拥有个性化的Gravatar头像

智能摘要 DeepSeek WordPress后台无上传头像功能,可修改代码扩展。Gravatar是全球通用头像服务,用户可在官网申请头像后,将其关联邮箱,即 ...
  • WordPress 教程
  • Gravatar头像
7,219 5
suxing
suxing
2016-04-23 9:55:54 广东

推荐 WordPress菜单导航功能设置教程

智能摘要 DeepSeek 初次使用WordPress主题建站时,建议按以下顺序操作:新建分类目录、几篇文章、页面、菜单和小工具,调整功能选项。特别提醒不要在 ...
  • WordPress 教程
  • WordPress导航教程
  • WordPress导航设置
  • wordpress教程
  • WordPress菜单教程
  • WordPress菜单设置
12,059 2
suxing
suxing
2014-10-02 16:21:49 广东

推荐 在WordPress中插入表格

智能摘要 DeepSeek 在WordPress中使用vfilm主题时,插入表格无法显示的问题可以通过以下步骤解决: 1. 在主题的`style.css` ...
  • WordPress 教程
  • 在WordPress中插入表格
  • 文章插入表格
16,830 5
suxing
suxing
2014-07-06 0:45:44 广东

推荐 WordPress去掉链接中category分类标志

智能摘要 DeepSeek 这段代码旨在去除WordPress主题中的category分类标志,例如将`suxing.me/category/217.html ...
  • WordPress 教程
  • Wordpress去掉category
  • 去掉链接中category
7,546 1
suxing
suxing
2024-07-11 14:43:03 广东

推荐 解决 wordpress 网站频繁出现 502 bad gateway 报错

WordPress网站出现”502 Bad Gateway”错误常见原因及解决方法如下: 1. **服务器过载**:资源不足、重启服务器 2. **网关或代理服务器问题**:Nginx配置错误、检查上游服务器 3. **PHP超时**:修 ...
  • WordPress 教程
  • WordPress 502 Bad Gateway
  • WordPress服务器配置
  • WordPress服务器问题
3,912 0

我用的 WordPress 主题

2021 年单栏 WordPress 主题

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