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

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

if( !class_exists('CustomPostTypeArchiveInNavMenu') ) { class CustomPostTypeArchiveInNavMenu { function CustomPostTypeArchiveInNavMenu() { add_action( 'admin_head-nav-menus.php', array( &$this, 'cpt_navmenu_metabox' ) )...
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();
}

原文链接:https://www.dqzy.cn/2025/05/20/675.html,转载请注明出处。 1、本站所有源码资源(包括源代码、软件、学习资料等)仅供研究学习以及参考等合法使用,请勿用于商业用途以及违法使用。如本站不慎侵犯您的版权请联系我们,我们将及时处理,并撤下相关内容! 2、访问本站的用户必须明白,本站对所提供下载的软件和程序代码不拥有任何权利,其版权归该软件和程序代码的合法拥有者所有,请用户在下载使用前必须详细阅读并遵守软件作者的“使用许可协议”,本站仅仅是一个学习交流的平台。 3、如下载的压缩包需要解压密码,若无特殊说明,那么文件的解压密码则为:www.dqzy.cn。 4、点启资源网是一个免费且专业分享网站源码、图片素材、特效代码、教程文章、站长工具的平台。我们努力给站长提供好的资源!
0

评论0

请先
显示验证码
没有账号?注册  忘记密码?