Greetings! I'm Aneesh Sreedharan, CEO of 2Hats Logic Solutions. At 2Hats Logic Solutions, we are dedicated to providing technical expertise and resolving your concerns in the world of technology. Our blog page serves as a resource where we share insights and experiences, offering valuable perspectives on your queries.
In WordPress menu section, it’s difficult for normal users to add links to the custom post archive page because, there are no options to add custom post archive page link like post, page and categories. If user wants to add an archive link, then the user must need to use custom link feature and it is very difficult. If there is an option like post or page links then it is very easy to manage links. So here describes how to add custom post archive page links to the WP menu section like post and page links.
Please use the given code in your functions.php of the theme. Then check your WP menu page from dashboard. You can see an additional met box for listing all custom post links. Then you can just select the custom post and add it to the menus.
Done !!. It is very easy!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | /** * Insert your code here */ if( !class_exists('CustomPostTypeArchiveInNavMenu') ) { class CustomPostTypeArchiveInNavMenu { function CustomPostTypeArchiveInNavMenu() { add_action( 'admin_head-nav-menus.php', array( &$this, 'rio_wpmenu_metabox' ) ); add_filter( 'wp_get_nav_menu_items', array( &$this,'rio_archive_menu_filter'), 10, 3 ); } function rio_wpmenu_metabox() { add_meta_box( 'add-rio-meta', __('Custom Post Archives'), array( &$this, 'rio_wpmenu_metabox_content' ), 'nav-menus', 'side', 'default' ); } function rio_wpmenu_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 . ' ' . __( 'Archive', 'riomedia' ); $post_type->object = 'rio-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="' . __('Add to Menu', 'riomedia') . '" name="add-rio-archive-menu-item" id="submit-cpt-archive" /><span class="spinner"></span>'; echo '</span>'; echo '</p>'; endif; } function rio_archive_menu_filter( $items, $menu, $args ) { foreach( $items as &$item ) { if( $item->object != 'rio-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; } } /* create object for class */ $CustomPostTypeArchiveInNavMenu = new CustomPostTypeArchiveInNavMenu(); } |