如果某个自定义帖子类型的最新帖子不超过一周,我想将 CSS 类添加到特定的导航菜单项 (li)。
这是我到目前为止得到的。它工作正常,但将 CSS 类添加到所有菜单项。如何通过 id 定位特定的 li?
function blog_menu_item_new_posts( $classes, $item ) {
global $wpdb;
$latest_post = $wpdb->get_var( "SELECT post_date FROM $wpdb->posts WHERE post_type = 'blogposts' AND post_status = 'publish' ORDER BY ID DESC LIMIT 0,1" );
$latest_post_date = strtotime($latest_post);
$threshold = strtotime("-1 week");
if ( $latest_post_date >= $threshold ) {
array_push( $classes, "new-posts" );
}
return $classes;
}
add_filter( "nav_menu_css_class", "blog_menu_item_new_posts", 10, 2 );