0

I'm Wordpress newbie and despite of reading many articles, I'm able to put archive years to my main menu.

Menu contains pages and I need to add an archive.

My current menu:

- Home (page)
- About (page)
- News (Category)
- Contact (page)

And I need

- Home (page)
- About (page)
- News (Category) - link to the newest year (2015)
- - 2015 (not category, just year when posts were added)
- - 2014
- - 2013
- Contact (page)

When I removed the last one category from year 2014, this year should disappear from my navigation.

When I add new post in January 2016, automatically year 2016 will appear in my dropdown, and 'News' will containt direct link to 2016.

Any idea how to do it?
PS. I really read many articles and see the WP first time (I'm a programmer). I'm able to do it using many ways, but looking for the best one, looking the best wordpress approach.

Thanks.

1 Answer 1

1

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'              => '/'.$year->year  // to create url of menu item 
      );
    $items[] = (object) $link;
  }
  return $items;    
}

RESULT:

enter image description here

Other Case like you want :

Just use like below:

'title' =>($year->year == date('Y') ) ? 'News (Category)' : $year->year,

enter image description here

Thanks!

8
  • Hi @jas, it overwrites me whole menu to just years taken from posts. Now I see just 2015 - 2014 - 2013, not other pages... Any idea what I did incorrectly?
    – Roman
    Nov 1, 2015 at 14:57
  • Are you using this code in same way, I have updated image, for me its displaying all other menu items and submenu under test are archives, please provide some details.
    – jas
    Nov 1, 2015 at 15:32
  • Yes, I copied whole your code to functions.php... I can provide you FTP access if you want to look on it. I don't know what more details I can tell you :-/ Maybe... when I comment the first line, it does nothing (show my current menu without subitems). When I uncomment it, see just years.
    – Roman
    Nov 1, 2015 at 15:36
  • now I tried to find function name from the 1st line, wpa_remove_menu_item, but this string is nowhere in my whole website folder... It seems like function name you have somewhere and I haven't... Can be the problem here?
    – Roman
    Nov 1, 2015 at 15:42
  • Please remove that first line and check now with updated answer, I think I copied some extra line from my file while pasting answer here.
    – jas
    Nov 1, 2015 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.