Recently Published Badge Snippet

This PHP code snippet is designed to automatically display a 'Recently Published' badge on WordPress posts that were published within the last 48 hours. It works by comparing the post's publication time with the current time and adding a badge to the post if it meets the criteria.

<?php // get the post date $post_date = get_the_date('Y-m-d H:i:s'); // convert post date to timestamp $post_timestamp = strtotime($post_date); // get current date and time $current_timestamp = time(); // calculate the difference in hours $difference_in_hours = ($current_timestamp - $post_timestamp) / 3600; // if the difference is less than or equal to 48 hours, display the badge if($difference_in_hours <= 48) { echo '<span class="badge">Recently Published</span>'; } ?>

Frequently Asked Questions

It calculates the time difference between the post's publication time and the current time to determine if a 'Recently Published' badge should be displayed.