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

  • A+
所属分类:Wordpress
/**
    *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' );

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: