最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Auto add (multiple) Custom Post Types to menu

programmeradmin0浏览0评论

I'm trying to add custom post types entries automatically as submenu's to my main menu. I've managed to add one CPT "nestjes" by using the code below.

Now I would like to add two more CPT's automatically to my menu on the same website: "poezen" and "katers". How do I blend in these variables in the code below?

  • my_theme_poezen_menu_filter

  • poezen-parent-item

  • poezen-post-type

  • my_theme_katers_menu_filter

  • katers-parent-item

  • katers-post-type

Thank you for the help.

add_filter( 'wp_get_nav_menu_items', 'my_theme_nestjes_menu_filter',10, 3 );

function my_theme_nestjes_menu_filter( $items, $menu, $args ) {
  $child_items = array(); 
  $menu_order = count($items); 
  $parent_item_id = 0; 

  
  foreach ( $items as $item ) {
    if ( in_array('nestjes-parent-item', $item->classes) ){
        $parent_item_id = $item->ID;
    }
  }

  if($parent_item_id > 0){

      foreach ( get_posts( 'post_type=nestjes-post-type&numberposts=-1' ) as $post ) {
        $post->menu_item_parent = $parent_item_id;
        $post->post_type = 'nav_menu_item';
        $post->object = 'custom';
        $post->type = 'custom';
        $post->menu_order = ++$menu_order;
        $post->title = $post->post_title;
        $post->url = get_permalink( $post->ID );
        array_push($child_items, $post);
      }

  }

  return array_merge( $items, $child_items );
}
发布评论

评论列表(0)

  1. 暂无评论