0

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

0

If I understand you question clearly that we just need to get post in that submenus from only cat id = 4 , this will make your archive.php to work only for cat id = 4

Please add below code to functions.php:

function wpse75668_filter_pre_get_posts( $query ) {


   if ( $query->is_main_query() && ! is_admin() ) {

      if ( is_date() ) {

        $query->set( 'cat', '4' );
     }
   }
 }
 add_action( 'pre_get_posts', 'wpse75668_filter_pre_get_posts' );

Thanks!

  • You are great, really. I see everything what I need is to modify functions.php, but I don't know the correct filters, vars, etc. It seems to work, my friend! – Roman Nov 2 '15 at 16:54
  • I am glad it helped :) – jas Nov 2 '15 at 16:55
0

I cannot comment so sorry . Why don't you try using WP_QUERY .

You can learn about WP_Query either from codex or personally i like tutsplus mastering wp_query.

http://code.tutsplus.com/tutorials/mastering-wp_query-an-introduction--cms-23023

You are using $wpdb to fetch data from database. But i think it is probably not the best way although it works :)

  • thanks for your feedback, but I'm not a WP dev and this code showed me anybody in here and works (makes what I needed). Don't know how to try using my WP QUERY. Any idea? – Roman Nov 2 '15 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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