在使用wordpress网站建设也有一段时间了,在使用过程中也遇到各种功能上的需求但是对于一些不懂写程序代码的同学来说想增加一个功能模块那是不可能独立完成的,所以搜集了一些常用功能自己也就当做个备忘录也好,同时也搜集一些wordpress优化代码这样可以让程序运行速度更快一些,一直没机会写这里专门抽出时间来写此文章,这里推荐新手刚刚接触的同学使用。
一、wordpress禁止修订功能
这个功能就是记录了一些文章编辑的记录内容,每次编辑一次文章都会有相应的记录很贴心,但是本人觉得这个功能对数据库会造成冗余,加上日积月累数据库就越来越大造成数据库的臃肿,所以有必要禁用此功能:
1,打开wp-includes/default-filters.php文件,找到以下代码:
[code]add_action( ‘pre_post_update’, ‘wp_save_post_revision’ );[/code]
这段语句主要是在每次更新文章时,调用’pre_post_update’ 这个函数,来创建修订版,所以在这段语句前添加 ‘//’
将其转为注释,这样就不必执行这个函数了。
[code]//add_action( ‘pre_post_update’, ‘wp_save_post_revision’ );[/code]
2,打开wp-admin/includes/post.php文件,找到以下代码:
[code]return _wp_put_post_revision( $_POST, true );[/code]
修改为:
[code]return edit_post();[/code]
这样以后每次在修改已发布文章时,就不再会出现修订版了,而且保留了自动保存草稿的功能。不过缺点就是,每次升级Wordpress都需要重新修改。
二、wordpress最新评论代码
就是在首页侧边栏处添加最新的评论可以显示在上面,这个小编发现很多人都在使用所以就搜集过来了。
[code]<h2>Recent Comments</h2>
<ul>
<?php
global $wpdb;
$sql = “SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author,
comment_date_gmt, comment_approved, comment_type,comment_author_url,comment_author_email, SUBSTRING
(comment_content,1,16) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb-
>comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = ‘1’ AND comment_type = ” AND
post_password = ” AND user_id=’0′ ORDER BY comment_date_gmt DESC LIMIT 10″;
$comments = $wpdb->get_results($sql);
$output = $pre_HTML;
foreach ($comments as $comment) {$output .= “\n<li>”.get_avatar(get_comment_author_email
(‘comment_author_email’), 24).strip_tags($comment->comment_author).”:<br />” . ” <a href=\”” .
get_permalink($comment->ID) .”#comment-” . $comment->comment_ID . “\” title=\”on ” .$comment-
>post_title . “\”>” . strip_tags($comment->com_excerpt).”</a>…</li>”;}
$output .= $post_HTML;
echo $output;?>
</ul>[/code]
说明:comment_content,1,16 中的16是每个留言的文字摘取数量;……ORDER BY comment_date_gmt DESC LIMIT 10中的10是留言数量
最好也用CSS代码美化一下:
[code]#sidebar img.avatar{float:left;position:relative;border:1px solid #ddd;padding:1px;margin-
right:5px;}[/code]
三、wordpress显示最新文章动态
这个功能很有必要,因为每次更新文章的时候随时可以让访问者第一时间内在首页中看到,不仅如此还有利有wordpress网站优化。
[code]<ul class=”list”>
<?php query_posts(array(‘posts_per_page’ => 20,’caller_get_posts’ =>1,’orderby’
=>date,));
while ( have_posts() ) : the_post(); ?>
<li><span class=”contents”><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”<?php the_title();
?>”><?php the_title(); ?></a></span><span class=”time”><?php the_time(‘m-d’); ?></span></li>
<?php endwhile;wp_reset_query();?>
</ul>[/code]
添加到需要显示的地方即可。
四、wordpress随机显示文章代码
这个功能可以有,这样是为了考虑一些文章被沉下去给老文章有显示到首页的机会每次刷新都可以随机显示出不同的文章,也适用于用户体验。
[code]<ul class=”list”>
<?php query_posts(array(‘posts_per_page’ => 10,’caller_get_posts’ =>1,’orderby’
=>rand,));
while ( have_posts() ) : the_post(); ?>
<li><span class=”contents”><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”<?php the_title();
?>”><?php the_title(); ?></a></span><span class=”time”><?php the_time(‘m-d’); ?></span></li>
<?php endwhile;wp_reset_query();?>
</ul>[/code]
五、wordpress显示最热文章代码
这个代码是绑定了评论功能的,哪篇文章评论的次数多就是显示在此模块上。
[code]<ul class=”list”>
<?php query_posts(array(‘posts_per_page’ => 10,’caller_get_posts’ =>1,’orderby’
=>comment_count,));
while ( have_posts() ) : the_post(); ?>
<li><span class=”contents”><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”<?php the_title();
?>”><?php the_title(); ?></a></span><span class=”time”><?php the_time(‘m-d’); ?></span></li>
<?php endwhile;wp_reset_query();?>
</ul>[/code]
六、wordpress显示本月评论最多的网友
这里是统计出本月评论最多的网友代码如下 :
[code]<?php //本月评论最多的朋友
$identity=”comment_author”;
$passwordpost = ” AND post_password=””;
$userexclude = ” AND user_id=’0′”;
$approved = ” AND comment_approved=’1′”;
$interval = 30;
$shownumber = 12;
$counts = $wpdb->get_results(“SELECT COUNT(” . $identity . “) AS cnt, comment_author,
comment_author_url,comment_author_email
FROM (SELECT * FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts
ON ($wpdb->posts.ID=$wpdb->comments.comment_post_ID)
WHERE MONTH(comment_date)=MONTH(now()) and YEAR(comment_date)=YEAR(now())” .
$userexclude . $passwordpost . $approved . “) AS tempcmt
GROUP BY ” . $identity . ” ORDER BY cnt DESC LIMIT ” . $shownumber); ?>
<h2>本月最活跃的朋友</h2>
<ul class=”ffox_most_active”>
<?php if ( $counts ) : foreach ($counts as $count) :
echo ‘<li class=”mostactive”>’ . ‘<a href=”‘. $count->comment_author_url .
‘” title=”‘ . $count->comment_author . ‘ (‘. $count->cnt . ‘评论)”>’ .get_avatar($count-
>comment_author_email,40).'</a></li>’;
endforeach; endif;
?>
</ul>[/code]
说明:$shownumber 是显示的评论作者数量,因为每周的算法不准确,所以其他参数就不说明了。
七、wordpress不同页面显示不同的文章数量。
这个功能很实用可以为分类页面显示不同的文章数量。
1,那么打开主题文件 archive.php,找到下面这句
[code]<?php if (have_posts()) : while (have_posts()) : the_post(); ?>[/code]
修改成:
[code]<?php $posts = query_posts($query_string . ‘&orderby=date&showposts=30’); ?><!–控制文章显示数量
,30即为文章数量–>[/code]
八、wordpress防止垃圾留言信息
这里很多人肯定需要到插件来实现此功能其实不需要这么麻烦。
将下面代码 copy 到 Function.php 最后
[code]<?php
/* ———————————————–
<<小牆>> Anti-Spam v1.7 by Willin Kan.
*/
//建立
class anti_spam {
function anti_spam() {
if (!current_user_can(‘level_0’)) {
add_action(‘template_redirect’,array($this,’w_tb’),1);
add_action(‘init’,array($this,’gate’),1);
add_action(‘preprocess_comment’,array($this,’sink’),1); } }
//設欄位
function w_tb() {
if (is_singular()) {
ob_start(create_function(‘$input’,’return preg_replace(“#textarea(.*?)name=([“‘])comment([“‘])(.+)
</textarea>#”,”textarea$1name=$2w$3$4</textarea>
<textarea name=”comment” cols=”100%” rows=”8″ style=”position:absolute;top:-500px;”></textarea>”,
$input);’));
} }
//檢查
function gate() {
if (isset($_POST[‘w’]) && !empty($_POST[‘w’]) && empty($_POST[‘comment’])) {
$_POST[‘comment’]= $_POST[‘w’];unset($_POST[‘w’]);
} else {$_POST[‘spam_confirmed’] = 1;}
}
//處理
function sink($comment) {
if (isset($_POST[‘spam_confirmed’]) && !empty($_POST[‘spam_confirmed’])) {
//方法一:直接擋掉, 將 die(); 前面兩斜線刪除即可.
//die();
//方法二:標記為spam, 留在資料庫檢查是否誤判.
add_filter(‘pre_comment_approved’,create_function(”,’return “spam”;’));
$is_ping = in_array($comment[‘comment_type’], array(‘pingback’, ‘trackback’));
if ($is_ping) {
$comment[‘comment_content’] = “◎ 這是 Pingback/Trackback, 小牆懷疑這可能是 Spam!n” .$comment
[‘comment_content’];
} else {
$comment[‘comment_content’] = “[ 小牆判斷這是Spam! ]n” .$comment[‘comment_content’];
}}
return $comment;
}}
$anti_spam = new anti_spam();
// — END —————————————-
?>[/code]
代码简单功能强大。
九、wordpress公告栏增加
如果是用wordpress来实现企业功能的话这功能还是很实用的,一般公司企业都需要这功能。
1. 把下面的代码加入所用主题的 Function.php,mg12 的代码里有大量注释,所以不用我多说,如果你只要一个公告功能直接使用就可以了。
[code]<?php
/**
* 选项组类型
*/
class ClassicOptions {
/* — 获取选项组 — */
function getOptions() {
// 在数据库中获取选项组
$options = get_option(‘classic_options’);
// 如果数据库中不存在该选项组, 设定这些选项的默认值, 并将它们插入数据库
if (!is_array($options)) {
$options[‘notice’] = false;
$options[‘notice_content’] = ”;
// TODO: 在这里追加其他选项
update_option(‘classic_options’, $options);
}
// 返回选项组
return $options;
}
/* — 初始化 — */
function init() {
// 如果是 POST 提交数据, 对数据进行限制, 并更新到数据库
if(isset($_POST[‘classic_save’])) {
// 获取选项组, 因为有可能只修改部分选项, 所以先整个拿下来再进行更改
$options = ClassicOptions::getOptions();
// 数据限制
if ($_POST[‘notice’]) {
$options[‘notice’] = (bool)true;
} else {
$options[‘notice’] = (bool)false;
}
$options[‘notice_content’] = stripslashes($_POST[‘notice_content’]);
// TODO: 在这追加其他选项的限制处理
// 更新数据
update_option(‘classic_options’, $options);
// 否则, 重新获取选项组, 也就是对数据进行初始化
} else {
ClassicOptions::getOptions();
}
// 在后台 Design 页面追加一个标签页, 叫 Current Theme Options
add_theme_page(“Current Theme Options”, “Current Theme Options”, ‘edit_themes’, basename(__FILE__),
array(‘ClassicOptions’, ‘display’));
}
/* — 标签页 — */
function display() {
$options = ClassicOptions::getOptions();
?>
<form action=”#” method=”post” enctype=”multipart/form-data” name=”classic_form” id=”classic_form”>
<div class=”wrap”>
<h2><?php _e(‘Current Theme Options’, ‘classic’); ?></h2>
<!– 公告栏 –>
<table class=”form-table”>
<tbody>
<tr valign=”top”>
<th scope=”row”>
<?php _e(‘Notice’, ‘classic’); ?>
<br/>
<small style=”font-weight:normal;”><?php _e(‘HTML enabled’, ‘classic’) ?></small>
</th>
<td>
<!– 是否显示公告栏 –>
<label>
<input name=”notice” type=”checkbox” value=”checkbox” <?php if($options[‘notice’]) echo
“checked=’checked'”; ?> />
<?php _e(‘Show notice.’, ‘classic’); ?>
</label>
<br/>
<!– 公告栏内容 –>
<label>
<textarea name=”notice_content” cols=”50″ rows=”10″ id=”notice_content” style=”width:98%;font-
size:12px;” class=”code”><?php echo($options[‘notice_content’]); ?></textarea>
</label>
</td>
</tr>
</tbody>
</table>
<!– TODO: 在这里追加其他选项内容 –>
<!– 提交按钮 –>
<p class=”submit”>
<input type=”submit” name=”classic_save” value=”<?php _e(‘Update Options »’, ‘classic’); ?>” />
</p>
</div>
</form>
<?php
}
}
/**
* 登记初始化方法
*/
add_action(‘admin_menu’, array(‘ClassicOptions’, ‘init’));
?>[/code]
2. 调用方法直接引用 mg12 原话:要公告栏在首页上显示, 需要修改一下 index.php, 这个比较简单, 只是通过一些判断语句决定东西要不要显示出来而已. 当然, 你可以进行其他操作, 关键是获取到选项的值, 并对它们进行处理。
其实可以分为两步:1. 获取选项 (对每个 PHP 文件, 获取一次就行了, 可以在文件顶部执行) ;2. 对选项进行处理 (这里判断成立的话就将公告内容显示出来)
[code]<!– 获取选项 –>
<?php $options = get_option(‘classic_options’); ?>
<!– 如果用户选择显示公告栏, 并且公告栏有内容, 则显示出来 –>
<?php if($options[‘notice’] && $options[‘notice_content’]) : ?>
<div id=”notice”>
<div class=”content”><?php echo($options[‘notice_content’]); ?></div>
</div>
<?php endif; ?>[/code]
这样就有了一个公告功能,在 wp 后台主题栏多了一个选项,在里面可以添加公告栏内容。
十、wordpress表情符号
这是比较适合一些为自己网站添加个性的站长,同样非插件实现wordpress增加表情符号的功能。
1,创建smiley.php文件添加一下代码:
[code]<script type=”text/javascript” language=”javascript”>
/* <![CDATA[ */
function grin(tag) {
var myField;
tag = ‘ ‘ + tag + ‘ ‘;
if (document.getElementById(‘comment’) && document.getElementById(‘comment’).type == ‘textarea’) {
myField = document.getElementById(‘comment’);
} else {
return false;
}
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = tag;
myField.focus();
}
else if (myField.selectionStart || myField.selectionStart == ‘0’) {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
var cursorPos = endPos;
myField.value = myField.value.substring(0, startPos)
+ tag
+ myField.value.substring(endPos, myField.value.length);
cursorPos += tag.length;
myField.focus();
myField.selectionStart = cursorPos;
myField.selectionEnd = cursorPos;
}
else {
myField.value += tag;
myField.focus();
}
}
/* ]]> */
</script>
<a href=”javascript:grin(‘:?:’)”><img src=”<?php bloginfo(‘wpurl’); ?>/wp-
includes/images/smilies/icon_question.gif” alt=”” /></a>
<a href=”javascript:grin(‘:razz:’)”><img src=”<?php bloginfo(‘wpurl’); ?>/wp-
includes/images/smilies/icon_razz.gif” alt=”” /></a>
<a href=”javascript:grin(‘:sad:’)”><img src=”<?php bloginfo(‘wpurl’); ?>/wp-
includes/images/smilies/icon_sad.gif” alt=”” /></a>
<a href=”javascript:grin(‘:evil:’)”><img src=”<?php bloginfo(‘wpurl’); ?>/wp-
includes/images/smilies/icon_evil.gif” alt=”” /></a>
<a href=”javascript:grin(‘:!:’)”><img src=”<?php bloginfo(‘wpurl’); ?>/wp-
includes/images/smilies/icon_exclaim.gif” alt=”” /></a>
<a href=”javascript:grin(‘:smile:’)”><img src=”<?php bloginfo(‘wpurl’); ?>/wp-
includes/images/smilies/icon_smile.gif” alt=”” /></a>
<a href=”javascript:grin(‘:oops:’)”><img src=”<?php bloginfo(‘wpurl’); ?>/wp-
includes/images/smilies/icon_redface.gif” alt=”” /></a>
<a href=”javascript:grin(‘:grin:’)”><img src=”<?php bloginfo(‘wpurl’); ?>/wp-
includes/images/smilies/icon_biggrin.gif” alt=”” /></a>
<a href=”javascript:grin(‘:eek:’)”><img src=”<?php bloginfo(‘wpurl’); ?>/wp-
includes/images/smilies/icon_surprised.gif” alt=”” /></a>
<a href=”javascript:grin(‘:shock:’)”><img src=”<?php bloginfo(‘wpurl’); ?>/wp-
includes/images/smilies/icon_eek.gif” alt=”” /></a>
<a href=”javascript:grin(‘:???:’)”><img src=”<?php bloginfo(‘wpurl’); ?>/wp-
includes/images/smilies/icon_confused.gif” alt=”” /></a>
<a href=”javascript:grin(‘:cool:’)”><img src=”<?php bloginfo(‘wpurl’); ?>/wp-
includes/images/smilies/icon_cool.gif” alt=”” /></a>
<a href=”javascript:grin(‘:lol:’)”><img src=”<?php bloginfo(‘wpurl’); ?>/wp-
includes/images/smilies/icon_lol.gif” alt=”” /></a>
<a href=”javascript:grin(‘:mad:’)”><img src=”<?php bloginfo(‘wpurl’); ?>/wp-
includes/images/smilies/icon_mad.gif” alt=”” /></a>
<a href=”javascript:grin(‘:twisted:’)”><img src=”<?php bloginfo(‘wpurl’); ?>/wp-
includes/images/smilies/icon_twisted.gif” alt=”” /></a>
<a href=”javascript:grin(‘:roll:’)”><img src=”<?php bloginfo(‘wpurl’); ?>/wp-
includes/images/smilies/icon_rolleyes.gif” alt=”” /></a>
<a href=”javascript:grin(‘:wink:’)”><img src=”<?php bloginfo(‘wpurl’); ?>/wp-
includes/images/smilies/icon_wink.gif” alt=”” /></a>
<a href=”javascript:grin(‘:idea:’)”><img src=”<?php bloginfo(‘wpurl’); ?>/wp-
includes/images/smilies/icon_idea.gif” alt=”” /></a>
<a href=”javascript:grin(‘:arrow:’)”><img src=”<?php bloginfo(‘wpurl’); ?>/wp-
includes/images/smilies/icon_arrow.gif” alt=”” /></a>
<a href=”javascript:grin(‘:neutral:’)”><img src=”<?php bloginfo(‘wpurl’); ?>/wp-
includes/images/smilies/icon_neutral.gif” alt=”” /></a>
<a href=”javascript:grin(‘:cry:’)”><img src=”<?php bloginfo(‘wpurl’); ?>/wp-
includes/images/smilies/icon_cry.gif” alt=”” /></a>
<a href=”javascript:grin(‘:mrgreen:’)”><img src=”<?php bloginfo(‘wpurl’); ?>/wp-
includes/images/smilies/icon_mrgreen.gif” alt=”” /></a>
<br />[/code]
如何调用以上代码呢?
2,打开主题文件comments.php在textarea之前的适当位置加上:
[code]<?php include(TEMPLATEPATH . ‘/smiley.php’); ?>[/code]