Created
October 3, 2014 17:31
-
-
Save andrewlaskey/4aa5dd162d06a0249ec6 to your computer and use it in GitHub Desktop.
Add custom post type archive page to menu screen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//http://stackoverflow.com/questions/20879401/how-to-add-custom-post-type-archive-to-menu | |
add_action('admin_head-nav-menus.php', 'wpclean_add_metabox_menu_posttype_archive'); | |
function wpclean_add_metabox_menu_posttype_archive() { | |
add_meta_box('wpclean-metabox-nav-menu-posttype', 'Custom Post Type Archives', 'wpclean_metabox_menu_posttype_archive', 'nav-menus', 'side', 'default'); | |
} | |
function wpclean_metabox_menu_posttype_archive() { | |
$post_types = get_post_types(array('show_in_nav_menus' => true, 'has_archive' => true), 'object'); | |
if ($post_types) : | |
$items = array(); | |
$loop_index = 999999; | |
foreach ($post_types as $post_type) { | |
$item = new stdClass(); | |
$loop_index++; | |
$item->object_id = $loop_index; | |
$item->db_id = 0; | |
$item->object = 'post_type_' . $post_type->query_var; | |
$item->menu_item_parent = 0; | |
$item->type = 'custom'; | |
$item->title = $post_type->labels->name; | |
$item->url = get_post_type_archive_link($post_type->query_var); | |
$item->target = ''; | |
$item->attr_title = ''; | |
$item->classes = array(); | |
$item->xfn = ''; | |
$items[] = $item; | |
} | |
$walker = new Walker_Nav_Menu_Checklist(array()); | |
echo '<div id="posttype-archive" class="posttypediv">'; | |
echo '<div id="tabs-panel-posttype-archive" class="tabs-panel tabs-panel-active">'; | |
echo '<ul id="posttype-archive-checklist" class="categorychecklist form-no-clear">'; | |
echo walk_nav_menu_tree(array_map('wp_setup_nav_menu_item', $items), 0, (object) array('walker' => $walker)); | |
echo '</ul>'; | |
echo '</div>'; | |
echo '</div>'; | |
echo '<p class="button-controls">'; | |
echo '<span class="add-to-menu">'; | |
echo '<input type="submit"' . disabled(1, 0) . ' class="button-secondary submit-add-to-menu right" value="' . __('Add to Menu', 'andromedamedia') . '" name="add-posttype-archive-menu-item" id="submit-posttype-archive" />'; | |
echo '<span class="spinner"></span>'; | |
echo '</span>'; | |
echo '</p>'; | |
endif; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment