WordPress 添加自定義文章類型的存檔頁面到菜單

如果你的網站使用瞭自定義文章類型,你可能需要將它的存檔頁面添加到導航菜單,雖然你可以使用“鏈接”來添加:

0383-wpdaxue_com

但是這樣添加的鏈接有一個弊端:訪問這個自定義文章類型下的頁面時,沒辦法高亮這個菜單項。

其實,最好的效果就是在菜單備選中羅列已有的自定義文章類型,然後可以選擇添加至菜單:

add-custom-post-types-archive-to-nav-menus-1-wpdaxue_com

要實現這個功能,將下面的代碼添加到主題的 functions.php :

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
/**
 * WordPress 添加自定義文章類型的存檔頁面到菜單
 * https://www.wpdaxue.com/add-custom-post-types-archive-to-nav-menus.html
 */
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 添加自定義文章類型的存檔頁面到菜單
* https://www.wpdaxue.com/add-custom-post-types-archive-to-nav-menus.html
*/
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();
}

通過這個方法添加的自定義文章類型的存檔頁面,隻要是屬於這個文章類型的頁面,都會自動在菜單中添加一個 current-menu-item:

1
<li class="current-menu-item"><a href="https://www.wpdaxue.com/questions">問題存檔</a></li>

<li class="current-menu-item"><a href="https://www.wpdaxue.com/questions">問題存檔</a></li>

配合 CSS 即可實現當前菜單項的高亮效果:

add-custom-post-types-archive-to-nav-menus-wpdaxue_com

如果你在主題或外掛開發中使用瞭自定義文章類型,建議將該功能集成到你的主題和外掛中,方便用戶使用。

以上代碼來自 Custom Post Type’s Archive in WP Nav Menu 外掛,如果你不想折騰代碼,安裝該外掛即可。

發佈留言