I'm trying to have a 'new' badge display on my menu beside the "Blog" menu item after a new blog post, and hide after 3 weeks if there's no new posts.. to let visitors know when there's a new post. The badge is a png.
I've looked for a new-post hook, but can't find anything with a time limit.
CodePudding user response:
This might get you going, put it in your functions.php file, edit as you see fit:
function add_icon_to_menu_item( $menu_items ) {
$menu_title_search = 'Blog';
$icon_html = '<img src="new_icon.png">';
$last_post = strtotime( get_lastpostdate() );
$new_icon_limit = strtotime( ' 3 weeks', $last_post ) - strtotime( 'now' );
foreach ( $menu_items as $menu_item ) {
if ( $menu_item->title === $menu_title_search && ( $new_icon_limit > 0 ) ) {
$menu_item->title = "$menu_title_search $icon_html";
}
}
return $menu_items;
}
add_filter('wp_nav_menu_objects', 'add_icon_to_menu_item', 10, 2);