2013-03-26 27 views
0

如果特定自定義帖子類型的最新帖子不超過一週,我想將CSS類添加到特定的導航菜單項(li)。特定列表項的CSS類

這就是我到目前爲止。它工作正常,但將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); 

回答

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);