- This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
Viewing 2 posts - 1 through 2 (of 2 total)
- You must be logged in to reply to this topic.
Need Help? Lets get in touch with our Support Team
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;
?>
Monday – Friday: 8:00AM to 5:30PM
Saturday – Sunday: Closed
Our time zone: GMT+7