我正在嘗試在發布新博文后在“博客”選單項旁邊的選單上顯示“新”徽章,如果沒有新博文,則在 3 周后隱藏……以便訪問者知道何時有新博文。徽章是一個 png。

我已經尋找了一個新的帖子鉤子,但找不到任何有時間限制的東西。
uj5u.com熱心網友回復:
這可能會讓你開始,把它放在你的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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/328777.html
標籤:php WordPress的
