Edit Post Title when not older than 3 days
-
Hi,
I’ve been struggling with this problem for quite some time now and I’ve finally decided to make a post here on the forum.
The general idea of what I’m trying to do is create some code that automatically adds a NEW icon to the end of each post title if it is not older than 3 days. I have tried several different approaches to this including the approach below using a post_query. I have also tried the approach found on this website: Website but have had no luck.
The below code is the closest I have come to get this to work. However, it adds the icon to all posts if one post in that query is not older than 3 days.
function new_icon_title($title){ if (in_category('all-surgery') && is_page()){ $title = $title . ' <img src="https://maxcdn.icons8.com/Color/PNG/24/Ecommerce/new-24.png" title="New" width="24">'; } return $title; } function add_new_icon(){ $args = array( 'post_type' => 'post', 'category_name' => 'all-surgery', 'date_query' => array( 'after' => date('Y-m-d', strtotime('-3 days')) ) ); $post_query = new WP_Query($args); if($post_query->have_posts()){ while($post_query->have_posts()){ if('date_query'){ add_filter('the_title', 'new_icon_title'); return; } } } } add_action('init', 'add_new_icon');
Any help would be very much appreciated.
Thank you.
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Edit Post Title when not older than 3 days’ is closed to new replies.