Ecommerce Shopify WordPress Discussion

Display a NEW tag/icon on a 1 week old WordPress post

I'm trying to show an icon/text on new posts which are of 1 week old but it doesn't work. What am I doing wrong? I have been searching for a tutorial but couldn't find any either except this which shows for -2 years. I converted to -1 week which isn't working. <?php $args = array( 'post_type' => 'post', 'post_status' => 'publish', 'category_name' => 'whats-new', 'posts_per_page' => 10, ); $arr_posts = new WP_Query( $args ); if ( $arr_posts->have_posts() ) : while ( $arr_posts->have_posts() ) : $arr_posts->the_post(); ?> <article> <div class="news"> <a href="<?php the_permalink(); ?>"><h4><?php the_title(); ?></h4></a> <?php if( strtotime( $post->post_date ) < strtotime('-1 week') ) { echo ''; } else { echo '<span class="icon icon-new">new posts'; } ?> </div> </div> </article> <?php endwhile; wp_reset_postdata(); endif; ?>
Your PHP code looks good for retrieving posts with the category 'whats-new' and displaying them with a "NEW" icon for posts less than one week old. However, there are a couple of minor corrections you can make for better structure and readability. Also, the closing tag seems to be unnecessary and might be a typo. Here's an updated version of your code: <?php $args = array( 'post_type' => 'post', 'post_status' => 'publish', 'category_name' => 'whats-new', 'posts_per_page' => 10, ); $arr_posts = new WP_Query($args); if ($arr_posts->have_posts()) : while ($arr_posts->have_posts()) : $arr_posts->the_post(); ?> <article> <div class="news"> <a href="<?php the_permalink(); ?>"><h4><?php the_title(); ?></h4></a> <?php if (strtotime(get_the_date()) < strtotime('-1 week')) { echo ''; } else { echo '<span class="icon icon-new">NEW</span>'; } ?> </div> </article> <?php endwhile; wp_reset_postdata(); endif; ?>

December 30, 2023

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.