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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
    *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' );
原文链接:https://xiaohost.com/1452.html,转载请注明出处。
0

评论0

请先