This should sort you out. I’ve commented as much as possible 🙂
Place the code below in to your functions.php file.
function your_recent_posts_by_category_with_badge() {
// WP_Query arguments
$args = array(
'category_name' => 'catname-1, catname-2',
//'cat' => 5,
//'tag' => 'tagname',
//'tag_id' => 5,
'posts_per_page' => 10
);
// Set today's date
$the_date_today = date('r');
// Create WP_Query instance
$the_query = new WP_Query( $args );
// Check for posts
if ( $the_query->have_posts() ) {
// Set up HTML list & CSS classes for styling
echo '<ul class="recentcategorybadgeposts widget_recent_entries">';
// Loop through each post
// Default is latest posts
while ( $the_query->have_posts() ) {
$the_query->the_post();
// Compare post date from todays date
$each_post_date = get_the_time('r');
$difference_in_days = round( ( strtotime( $the_date_today ) - strtotime( $each_post_date) ) / ( 24 * 60 * 60 ), 0 );
// Condition set for 5 days
if ( $difference_in_days >= 5 ) {
// If less than 5 days show IMG tag
echo '<li><a href="' . get_the_permalink() .'">' . get_the_title() .'</a><img src="new.gif"></li>';
} else {
// Else no show
echo '<li><a href="' . get_the_permalink() .'">' . get_the_title() .'</a></li>';
}
} // end while
// Close list
echo '</ul>';
} // endif
// Reset Post Data
wp_reset_postdata();
}
// Add a shortcode to use anywhere within your website
add_shortcode('recentcategorybadgeposts', 'your_recent_posts_by_category_with_badge');
// Enable shortcodes in text widgets
// Add a 'Text Widget' to your sidebar then in the input field, paste [recentcategorybadgeposts]
add_filter('widget_text', 'do_shortcode');