New Posts for Returning Visitors

Fully admit rookie & I’ve tried several different trial and error attempts with no luck.

So I found this code to mark new posts for returning users:

[php]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 .= ‘New’;
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+60247, COOKIEPATH, COOKIE_DOMAIN );
}

add_action( ‘init’, ‘wpb_lastvisit_set_cookie’ );
[/php]
I have added it to the site and it works. However, I’m trying to move the “New” to be at the beginning of the title. I tried swapping the two strings but it doesn’ work (i.e.)
[php]if ($publish_date > $lastvisit) ‘New’ .= $title;[/php]
So is it possible to put the “New” at the beginning of the title? Why won’t switching the two strings in the above if statement work?

Thank you in advance.

You need to understand what .= actually does.

It adds whatever is in this line to the end of whatever is already in $title.

So what you need to do is add your span before the $title not after it.

Sponsor our Newsletter | Privacy Policy | Terms of Service