How to add an image dynamically to post title if it is published today or less than 2 days
Question
I was trying to add an image like a “new” image to the posts which are just published and are less than 2 days old.
I tried using a function like this within a WP_Query but it is applying to all posts.
add_action('the_title', 'insiderable_add_img_new');
function insiderable_add_img_new($title) {
$title = '<img src="http://insiderable.com/wp-content/uploads/2020/03/newicon.gif">'.$title;
return $title;
}
This is what I tried with WP_Query:
$events_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>-1));
if($events_query->have_posts()) :
while($events_query->have_posts()) :
$events_query->the_post();
if (get_the_date( 'Y-m-d' ) === date( 'Y-m-d' )) {
add_filter('the_title', 'insiderable_add_img_new');
}
endwhile; else: endif;
wp_reset_postdata();
function insiderable_add_img_new($title) {
$title = '<img src="http://insiderable.com/wp-content/uploads/2020/03/newicon.gif">'.$title;
return $title;
}
0
functions, php, themes, title, wp-query
3 years
2020-04-06T04:51:00-05:00
2020-04-06T04:51:00-05:00 0 Answers
84 views
0
Leave an answer