Hi Please follow below code do add dynamically year wise post under sub menu you want, I have added below code in functions.php
add_filter( 'wp_nav_menu_objects', 'ravs_add_menu_parent_class' );
function ravs_add_menu_parent_class( $items ) {
foreach ( $items as $item ) {
//print_r($item);//print each menu item an get your parent menu item-id
// get your menu item ID use that ID in below code and you can remove this code after getting ID
}
GLOBAL $wpdb;
$years = $wpdb->get_results( "SELECT YEAR(post_date) AS year FROM wp_posts WHERE post_type="post" AND post_status="publish" GROUP BY year DESC" );
foreach($years as $year){
$link = array (
'title' => $year->year,
// 'title' =>($year->year == date('Y') ) ? 'News (Category)' : $year->year, // this is how you want to print latest year as "News (Category)"
'menu_item_parent' => '13', // my menu id is 13 ie: ID of menu name test under which years links are displayed
'ID' => '',
'db_id' => '',
'url' => "https://wordpress.stackexchange.com/".$year->year // to create url of menu item
);
$items[] = (object) $link;
}
return $items;
}
RESULT:
Other Case like you want :
Just use like below:
'title' =>($year->year == date('Y') ) ? 'News (Category)' : $year->year,
Thanks!