0

特定のカスタム投稿タイプの最新の投稿が 1 週間以内の場合、特定のナビゲーション メニュー項目 (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 );
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 に答える