特定列表项的CSS类

时间:2013-03-26 19:02:19

标签: wordpress wordpress-theming

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

1 个答案:

答案 0 :(得分: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 );