wordpress收费下载资源主题
wordpress付费下载资源插件

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

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

0 个评论

定制开发
本站承接WordPress等系统建站仿站、二次开发、主题插件定制等开发服务
在线咨询
  • 请先加Q,临时会话收不到
    QQ:1-247-246-247

  • QQ一群:104228692(满)
  • QQ二群:64786792
在线咨询
本站承接WordPress建站仿站、二次开发、主题插件定制等PHP开发服务!

了解详情