WordPress教程

WordPress 添加自定义文章类型的存档页面到菜单

阿里云

如果你的网站使用了自定义文章类型,你可能需要将它的存档页面添加到导航菜单,虽然你可以使用“链接”来添加:
WordPress 添加自定义文章类型的存档页面到菜单
但是这样添加的链接有一个弊端:访问这个自定义文章类型下的页面时,没办法高亮这个菜单项。其实,最好的效果就是在菜单备选中罗列已有的自定义文章类型,然后可以选择添加至菜单:
WordPress 添加自定义文章类型的存档页面到菜单
要实现这个功能,将下面的代码添加到主题的 functions.php :

  1. /**
  2.  * WordPress 添加自定义文章类型的存档页面到菜单
  3.  */
  4. if( !class_exists('CustomPostTypeArchiveInNavMenu') ) {
  5. 	class CustomPostTypeArchiveInNavMenu {
  6. 		function CustomPostTypeArchiveInNavMenu() {
  7. 			add_action( 'admin_head-nav-menus.php', array( &$this, 'cpt_navmenu_metabox' ) );
  8. 			add_filter( 'wp_get_nav_menu_items', array( &$this,'cpt_archive_menu_filter'), 10, 3 );
  9. 		}
  10. 		function cpt_navmenu_metabox() {
  11. 			add_meta_box( 'add-cpt', __('自定义文章类型存档'), array( &$this, 'cpt_navmenu_metabox_content' ), 'nav-menus', 'side', 'default' );
  12. 		}
  13. 		function cpt_navmenu_metabox_content() {
  14. 			$post_types = get_post_types( array( 'show_in_nav_menus' => true, 'has_archive' => true ), 'object' );
  15. 			if( $post_types ) {
  16. 				foreach ( $post_types as &$post_type ) {
  17. 					$post_type->classes = array();
  18. 					$post_type->type = $post_type->name;
  19. 					$post_type->object_id = $post_type->name;
  20. 					$post_type->title = $post_type->labels->name . __( '存档' );
  21. 					$post_type->object = 'cpt-archive';
  22. 				}
  23. 				$walker = new Walker_Nav_Menu_Checklist( array() );
  24. 				echo '<div id="cpt-archive" class="posttypediv">';
  25. 				echo '<div id="tabs-panel-cpt-archive" class="tabs-panel tabs-panel-active">';
  26. 				echo '<ul id="ctp-archive-checklist" class="categorychecklist form-no-clear">';
  27. 				echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $post_types), 0, (object) array( 'walker' => $walker) );
  28. 				echo '</ul>';
  29. 				echo '</div><!-- /.tabs-panel -->';
  30. 				echo '</div>';
  31. 				echo '<p class="button-controls">';
  32. 				echo '<span class="add-to-menu">';
  33. 				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" />';
  34. 				echo '<span class="spinner"></span>';
  35. 				echo '</span>';
  36. 				echo '</p>';
  37. 			} else {
  38. 				echo '没有自定义文章类型';
  39. 			}
  40. 		}
  41. 		function cpt_archive_menu_filter( $items, $menu, $args ) {
  42. 			foreach( $items as &$item ) {
  43. 				if( $item->object != 'cpt-archive' ) continue;
  44. 				$item->url = get_post_type_archive_link( $item->type );
  45. 				if( get_query_var( 'post_type' ) == $item->type ) {
  46. 					$item->classes[] = 'current-menu-item';
  47. 					$item->current = true;
  48. 				}
  49. 			}
  50. 			return $items;
  51. 		}
  52. 	}
  53. 	$CustomPostTypeArchiveInNavMenu = new CustomPostTypeArchiveInNavMenu();
  54. }
也想出现在这里?联系我们
创客主机

通过这个方法添加的自定义文章类型的存档页面,只要是属于这个文章类型的页面,都会自动在菜单中添加一个 current-menu-item:

  1. <li class="current-menu-item"><a href="https://www.wpdaxue.com/questions">问题存档</a></li>

配合 CSS 即可实现当前菜单项的高亮效果,如果你在主题或插件开发中使用了自定义文章类型,建议将该功能集成到你的主题和插件中,方便用户使用。以上代码来自 Custom Post Type's Archive in WP Nav Menu 插件,如果你不想折腾代码,安装该插件即可。

WordPress 添加自定义文章类型的存档页面到菜单

已有 143 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!