[英]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.我正在尝试在发布新博文后在“博客”菜单项旁边的菜单上显示“新”徽章,如果没有新博文,则在 3 周后隐藏……以便访问者知道何时有新博文。 The badge is a png.
徽章是一个 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:这可能会让你开始,把它放在你的functions.php文件中,按照你认为合适的方式进行编辑:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.