0

I am trying to add "NEW" on top of my unread posts using cookies.

This is my current code but the span won't show up.

function wpb_lastvisit_the_title ( $title, $id ) {

    if ( !in_the_loop() || is_singular() || get_post_type( $id ) == 'page' )
        return $title;

    // if no cookie then just return the title 

    if ( !isset($_COOKIE['lastvisit']) ||  $_COOKIE['lastvisit'] == '' ) 
        return $title;

    $lastvisit = $_COOKIE['lastvisit'];
    $publish_date = get_post_time( 'U', true, $id );
    if ($publish_date > $lastvisit) 
        $title .= '<span class="new-article">New</span>';
    return $title;

}

add_filter( 'the_title', 'wpb_lastvisit_the_title', 10, 2);

// Set the lastvisit cookie 

function wpb_lastvisit_set_cookie() {

    if ( is_admin() ) 
        return;
    $current = current_time( 'timestamp', 1);
    setcookie( 'lastvisit', $current, time()+(60*60*24*7), COOKIEPATH, COOKIE_DOMAIN );
}

add_action( 'init', 'wpb_lastvisit_set_cookie' );

Does anyone know why my span does not show up?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.