if( !class_exists('CustomPostTypeArchiveInNavMenu') ) { class CustomPostTypeArchiveInNavMenu { function CustomPostTypeArchiveInNavMenu() { add_action( 'admin_head-nav-menus.php', array( &$this, 'cpt_navmenu_metabox' ) ); add_filter( 'wp_get_nav_menu_items', array( &$this,'cpt_archive_menu_filter'), 10, 3 ); } function cpt_navmenu_metabox() { add_meta_box( 'add-cpt', __('自定义文章类型存档'), array( &$this, 'cpt_navmenu_metabox_content' ), 'nav-menus', 'side', 'default' ); } function cpt_navmenu_metabox_content() { $post_types = get_post_types( array( 'show_in_nav_menus' => true, 'has_archive' => true ), 'object' ); if( $post_types ) { foreach ( $post_types as &$post_type ) { $post_type->classes = array(); $post_type->type = $post_type->name; $post_type->object_id = $post_type->name; $post_type->title = $post_type->labels->name . __( '存档' ); $post_type->object = 'cpt-archive'; } $walker = new Walker_Nav_Menu_Checklist( array() ); echo '<div id="cpt-archive" class="posttypediv">'; echo '<div id="tabs-panel-cpt-archive" class="tabs-panel tabs-panel-active">'; echo '<ul id="ctp-archive-checklist" class="categorychecklist form-no-clear">'; echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $post_types), 0, (object) array( 'walker' => $walker) ); echo '</ul>'; echo '</div><!-- /.tabs-panel -->'; echo '</div>'; echo '<p class="button-controls">'; echo '<span class="add-to-menu">'; echo '<input type="submit"' . disabled( $nav_menu_selected_id, 0 ) . ' class="button-secondary submit-add-to-menu right" value="'. __('添加至菜单') . '" name="add-ctp-archive-menu-item" id="submit-cpt-archive" />'; echo '<span class="spinner"></span>'; echo '</span>'; echo '</p>'; } else { echo '没有自定义文章类型'; } } function cpt_archive_menu_filter( $items, $menu, $args ) { foreach( $items as &$item ) { if( $item->object != 'cpt-archive' ) continue; $item->url = get_post_type_archive_link( $item->type ); if( get_query_var( 'post_type' ) == $item->type ) { $item->classes[] = 'current-menu-item'; $item->current = true; } } return $items; } } $CustomPostTypeArchiveInNavMenu = new CustomPostTypeArchiveInNavMenu(); }
来源地址:WordPress 添加自定义文章类型的存档页面到菜单
转载声明:本站文章若无特别说明,皆为原创,转载请注明来源:www.88531.cn资享网,谢谢!^^
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END