网站经常有些老文章更新,但是却没有提示,导致用户不知道或者需要重新置顶文章,这里给wordpress添加一个显示最近更新过文章的调用方法。
wordpress调用最近更新文章列表
1、将以下代码添加到主题的 functions.php里
function recently_updated_posts( n u m = 10 , num=10, num=10,days=7) {
if( !$recently_updated_posts = get_option(‘recently_updated_posts’) ) {
query_posts(‘post_status=publish&orderby=modified&posts_per_page=-1’);
$i=0;
while ( have_posts() && i < i< i<num ) : the_post();
if (current_time(‘timestamp’) - get_the_time(‘U’) > 606024*$days) {
$i++;
$the_title_value=get_the_title();
r e c e n t l y u p d a t e d p o s t s . = ′ < l i > < a h r e f = " ′ . g e t p e r m a l i n k ( ) . ′ " t i t l e = " ′ . recently_updated_posts.='
.KaTeX parse error: Expected 'EOF', got '&' at position 53: …atetime">
&̲raquo; 修改时间: ' …recently_updated_posts) ) update_option(‘recently_updated_posts’, $recently_updated_posts);
}
r e c e n t l y u p d a t e d p o s t s = ( recently_updated_posts=( recentlyupdatedposts=(recently_updated_posts == ‘’) ? ‘
function clear_cache_zww() {
update_option(‘recently_updated_posts’, ‘’); // 清空 recently_updated_posts
}
add_action(‘save_post’, ‘clear_cache_zww’); // 新发表文章/修改文章时触发更新
2、调用方法
以下代码中 8 为展示文章数量,15 指15天内发表的文章除外,具体使用的时候可以根据自己的情况修改这两个参数。
可以将调用代码放在边侧栏或者文章页面任何地方调用。
效果如下:wordpress邮件通知评论用户文章更新
将以下代码添加到functions.php中,会在修改编辑文章的页面添加一个选项,只有勾选才会邮件通知。
//修改更新文章时邮件通知评论用户
add_action( ‘submitpost_box’, ‘lxtx_fo_submit_box’);
function lxtx_fo_submit_box( ){
echo ‘
//修订版本不通知,以免滥用
if( wp_is_post_revision($post_ID) ){
return;
}
//获取wp数据操作类
global w p d b , wpdb, wpdb,post;
// 读数据库,获取文章的所有用户的email并且不重复
$emailauthor != ‘你自己的邮箱’;
$wp_user_emails = $wpdb->get_results(“SELECT DISTINCT comment_author, comment_author_email FROM w p d b − > c o m m e n t s W H E R E T R I M ( c o m m e n t a u t h o r e m a i l ) I S N O T N U L L A N D T R I M ( c o m m e n t a u t h o r e m a i l ) ! = ′ ′ A N D T R I M ( c o m m e n t a u t h o r e m a i l ) ! = ′ wpdb->comments WHERE TRIM(comment_author_email) IS NOT NULL AND TRIM(comment_author_email) != '' AND TRIM(comment_author_email) != ' wpdb−>commentsWHERETRIM(commentauthoremail)ISNOTNULLANDTRIM(commentauthoremail)!=′′ANDTRIM(commentauthoremail)!=′emailauthor’ AND comment_post_ID = $post->ID”);
// 获取博客名称
$blogname = wp_specialchars_decode(get_option(‘blogname’), ENT_QUOTES);
// 获取博客URL
$blogurl = get_bloginfo(“siteurl”);
//文章链接
p o s t l i n k = g e t p e r m a l i n k ( post_link = get_permalink( postlink=getpermalink(post_ID);
//文章标题$post -> post_title
p o s t t i t l e = s t r i p t a g s ( post_title = strip_tags( posttitle=striptags(_POST[‘post_title’]);
//文章内容$post->post_content
p o s t c o n t e n t = s t r i p t a g s ( post_content = strip_tags( postcontent=striptags(_POST[‘post_content’]);
//文章摘要
KaTeX parse error: Undefined control sequence: \x at position 30: …replace('/^(?:[\̲x̲00-\x7F]|[\xC0-…post_content).’…’;
//邮件头,以免乱码
$message_headers = “Content-Type: text/html; charset=“utf-8”\n”;
// 邮件标题
s u b j e c t = ′ 您 曾 经 来 访 过 的 《 ′ . subject = '您曾经来访过的《'. subject=′您曾经来访过的《′.post_title.’》文章更新通知。’;
foreach ( $wp_user_emails as $wp_user_email )
{
// 邮件内容
$message = ’
您曾经来访过的《'.$post_title.'》有更新,博主觉得有必要通知您,希望不会骚扰到您。
文章标题:'.$post_title.'
文章摘要:'.$output.'
您可以点击链接 '.$blogname.' > '.$post_title.' 详细查看
来自: '.$blogname.'