How to add custom post archive page links to WP Menus like page and post links

PHPWordPress
How to add custom post archive page links to WP Menus like page and post links

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!

/**
 * 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 '
'; echo '
'; echo '
    '; echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $post_types), 0, (object) array( 'walker' => $walker) ); echo '
'; echo '
'; echo '
'; echo '

'; echo ''; echo ''; echo ''; echo '

'; 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(); }

Leave a Reply

Your email address will not be published. Required fields are marked *

four × 1 =

2hats Logic HelpBot