非插件实现WordPress提醒访客上次来访之后更新的文章

作者: 站长 上传时间: 浏览: N/A 下载: N/A 格式: N/A 评分: N/A

/**
*WordPress 提醒访客上次来访后更新的文章
*/
function Bing_lastvisit_the_title( $title, $id ){
if( !in_the_loop() || is_singular() || get_post_type( $id ) == 'page' ) return $title;
$cookiename = COOKIEHASH . '_lastvisit';
if( !isset( $_COOKIE[$cookiename] ) || empty( $_COOKIE[$cookiename] ) ) return $title;
$lastvisit = $_COOKIE[$cookiename];
$publish_date = get_post_time( 'U', true, $id );
if( $publish_date > $lastvisit ) $title .= '(新)';
return $title;
}
add_filter( 'the_title', 'Bing_lastvisit_the_title', 12, 2 );

//设置 Cookie
function Bing_lastvisit_set_cookie(){
if( is_admin() ) return;
$current = current_time( 'timestamp', 1 );
setcookie( COOKIEHASH . '_lastvisit', $current, time() + 60 + 60 * 24 * 7, COOKIEPATH, COOKIE_DOMAIN );
}
add_action( 'init', 'Bing_lastvisit_set_cookie' );

Leave a Comment