0

如果某个自定义帖子类型的最新帖子不超过一周,我想将 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 );
4

1 回答 1

0

这是通过 id 仅针对特定菜单项的工作代码(在本例中为 id 101):

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 ) && ($item->ID == 101) ) {
    array_push( $classes, "new-posts" );
   }

   return $classes;
}

add_filter( "nav_menu_css_class", "blog_menu_item_new_posts", 10, 2 );
于 2013-03-26T19:20:50.933 回答