简体   繁体   中英

Display 'new' badge on Blog menu item after new blog post

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM