yesterday @jas helped to how to show archive by years in my nav. It works fine, but there is the second one little problem.
The @jas's solution filter me posts from all categories by year, I'd need to add filter to just one category.
The function which add me years to my navigation is:
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' => '/'.$year->year // to create url of menu item
);
$items[] = (object) $link;
}
return $items;
}
Is there someone who helps me to extend it to category 'news' (if ID is needed, the cat's ID is '4').
Thanks for all.
Roman