貌似在 wp-config.php
中加入 define(’WP_POST_REVISIONS’, false);
来禁用 WordPress 的日志修订功能,post revision 还是会产生。gohsy 同学写了个插件 Revision Manager 来清理 post revision,不过个人觉得手动清理还是不够方便,决定利用 WordPress 的计划任务功能(WP_Cron)偷偷懒。
不想为了这小小的功能而多添加一个插件,所以在主题目录下的 functions.php
文件添加了以下代码:
function delete_post_revisions() {
global $wpdb;
// Also need to delete the post meta and term relationships
$wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id IN (SELECT ID FROM {$wpdb->posts} WHERE post_type = 'revision')");
$wpdb->query("DELETE FROM {$wpdb->term_relationships} WHERE object_id IN (SELECT ID FROM {$wpdb->posts} WHERE post_type = 'revision')");
// Delete the post revisions
$wpdb->query("DELETE FROM {$wpdb->posts} WHERE post_type = 'revision'");
}
// Register the event
add_action('delete_post_revisions_event', 'delete_post_revisions');
if (!wp_next_scheduled('delete_post_revisions_event')) {
wp_schedule_event(time(), 'daily', 'delete_post_revisions_event');
}
Continue reading…
在之前使用 HemingwayEx 主题的时候我把一些只有半句或几句话的日志移动到了 asides 分类,而现在我不想让 Google Sitemaps Generator 插件生成 sitemap 的时候包括这些日志,也许只有我才会有这样奇怪的需求 😯 。
可 Google XML Sitemaps 只能忽略指定的日志或页面,而不能忽略整个分类,所以只能自己动手了。
1. 打开插件目录下的 sitemap-core.php
文件,找到:
$where.=" AND post_password='' ORDER BY post_modified DESC";
$sql .= $where;
修改成:
$where.=" AND post_password='' ORDER BY post_modified DESC";
$where = apply_filters('sitemap_exclude_categories', $where);
$sql .= $where;
Continue reading…
之前用比较暴力的方式实现了分页文章的静态化,不过这样一来升级 WordPress 就不太方便了。厌烦了每次升级都要修改源文件,于是利用 WordPress 本身提供的接口实现了更好的解决方案。
以/%year%/%monthnum%/%postname%.html
这样的永久链接结构为例:
1. 打开主题目录下的functions.php
文件,添加以下代码:
// 添加分页处理规则
function add_custom_post_rewrite_rules($rules) {
$custom_rules = array(
'([0-9]{4})/([0-9]{1,2})/([^/]+)-([0-9]+)\.html$' => 'index.php?year=$matches[1]&monthnum=$matches[2]&name=$matches[3]&page=$matches[4]',
);
$rules = array_merge($custom_rules, $rules);
return $rules;
}
add_filter('post_rewrite_rules', 'add_custom_post_rewrite_rules');
// 修改分页链接
function my_wp_link_pages($args = '') {
$args .= ($args ? '&' : '') . 'echo=0';
$links = wp_link_pages($args);
$links = preg_replace_callback('|([0-9]{4}/[0-9]{1,2}/)([^/]+)(\.html)(/)([0-9]+)|', 'custom_page_link', $links);
echo $links;
}
function custom_page_link($matches) {
return $matches[1].$matches[2].'-'.$matches[5].$matches[3];
}
Continue reading…