IOM Partners of Houston

A comprehensive range of Houston Website Design Services now on offer from IOM Houston which is the market as well as segment leader in web design and development solutions through its professional team of website designers who possess years of industry s

How to Highlight New Posts on Your WordPress Site For Returning Visitors?

The popularity of your website depends on how well you can retain the attention of your visitors. If you can’t get return visitors and keep them glued to your site, you can’t expect to reach the heights of success in the online word. New visitors aren’t going to take you far! Therefore, you need to focus on retaining your visitors and keeping them interested in your site. One simple way of doing that is highlighting new posts that are posted on your site since the last visit of a particular user. This way the user would be able to find new and exciting posts on your site with ease and that would surely increase his interest in your site.


Now, the process of highlighting new posts is somewhat easy. However, the technique that we have described here involves editing certain files of your WordPress site and using certain codes. You should avoid any errors as they might stop your website from functioning properly. If you are not feeling confident enough, then you can take the help of the experts. Houstonians can take the help of the leading names in Website Design Houston as they are regarded as experts in WordPress web design.
The process


Okay, so let’s take a look at the process. To highlight the new posts, you just need to add the following code snippet to the functions.php file of your WordPress site. Make sure that you have a backup of your site before you add any code snippet. This would protect your site from any issues caused by errors.


01 function wpb_lastvisit_the_title ( $title, $id ) {
02
03 if ( !in_the_loop() || is_singular() || get_post_type( $id ) =='page' ) return $title;
04
05 // if no cookie then just return the title
06
07 if ( !isset($_COOKIE['lastvisit']) || $_COOKIE['lastvisit'] =='' ) return $title;
08 $lastvisit = $_COOKIE['lastvisit'];
09 $publish_date = get_post_time( 'U', true, $id );
10 if ($publish_date > $lastvisit) $title .= '<span class="new-article">New</span>';
11 return $title;
12
13 }
14
15 add_filter( 'the_title', 'wpb_lastvisit_the_title', 10, 2);
16
17 // Set the lastvisit cookie
18
19 function wpb_lastvisit_set_cookie() {
20
21 if ( is_admin() ) return;
22 $current = current_time( 'timestamp', 1);
23 setcookie( 'lastvisit', $current, time()+60+60*24*7, COOKIEPATH, COOKIE_DOMAIN );
24 }
25
26 add_action( 'init', 'wpb_lastvisit_set_cookie' );


What this code does is to look for a cookie called ‘lastvisit’ which tells about the last time a visitor has visited your site. If such a cookie is found, then this code adds the tag ‘New’ to every post published on your site since the last visit of the user.


You would also see that there is a new article class in the <span> tag around ‘New’. This class would be used to style the text using CSS.


You can simply copy paste the following code in your child theme’s stylesheet or you can use your own CSS code as well.


01 .new-article {
02 background: #feffdd;
03 padding: 3px;
04 border: 1px solid #eeefd2;
05 -webkit-border-radius: 5px;
06 -moz-border-radius: 5px;
07 border-radius: 5px;
08 margin-left:5px;
09 font-size: small;
10 font-weight: bold;
11 }


So this was the technique that you can use to highlight the newer posts in your website. Try to use this simple technique as this would prove to be really useful to retain the interest of the user and increase the popularity of your blog or website.