WordPress自定义文章类型分类链接添加至菜单问题求助
解决方案:自定义文章类型分类链接与后台导航菜单集成
问题1:生成仅显示自定义文章类型的分类链接
要获取只展示hotels(或travel-tips)自定义文章类型的分类链接,这里有两种实用思路:
方法1:直接追加post_type参数(快速实现)
默认分类链接是example.com/category/mexico/,我们只需要在末尾加上?post_type=hotels,就能过滤出该分类下的所有酒店文章。可以用下面的自定义函数快速生成这类链接:
function get_custom_post_type_category_link( $term_id, $post_type ) { $term = get_term( $term_id, 'category' ); if ( !$term ) return ''; $term_link = get_term_link( $term ); return add_query_arg( 'post_type', $post_type, $term_link ); }
使用示例:如果要获取ID为5的分类下的酒店链接,直接这么调用就行:
echo get_custom_post_type_category_link( 5, 'hotels' );
方法2:重写规则实现友好URL(可选优化)
如果你想要更美观的URL格式(比如example.com/category/mexico/hotels/),可以添加重写规则来实现。把下面的代码放到主题的functions.php里:
function custom_post_type_category_rewrite() { add_rewrite_rule( 'category/([^/]+)/([^/]+)/?$', 'index.php?category_name=$matches[1]&post_type=$matches[2]', 'top' ); } add_action( 'init', 'custom_post_type_category_rewrite' );
代码添加完成后,记得去设置→固定链接页面点击「保存更改」,刷新WordPress的重写规则,之后就能用友好URL访问分类下的指定自定义文章类型了。
问题2:后台导航菜单添加自定义分类+文章类型选项
要让用户在后台外观→菜单界面能直接添加「分类+自定义文章类型」的菜单项,我们需要扩展WordPress的导航菜单元框。下面是完整的实现代码,放到主题的functions.php即可:
// 添加自定义菜单项元框到后台菜单界面 function add_custom_post_type_category_menu_meta_box() { add_meta_box( 'custom_post_type_category_menu', '分类+自定义文章类型', 'render_custom_post_type_category_menu_meta_box', 'nav-menus', 'side', 'default' ); } add_action( 'admin_init', 'add_custom_post_type_category_menu_meta_box' ); // 渲染元框内容 function render_custom_post_type_category_menu_meta_box() { global $nav_menu_selected_id; // 获取所有分类和你的自定义文章类型 $categories = get_categories( array( 'hide_empty' => false ) ); $post_types = array( 'hotels' => '酒店', 'travel-tips' => '旅行贴士' ); ?> <div id="custom-post-type-category" class="posttypediv"> <div id="tabs-panel-custom-post-type-category" class="tabs-panel tabs-panel-active"> <ul id="custom-post-type-category-checklist" class="categorychecklist form-no-clear"> <?php foreach ( $categories as $category ) { foreach ( $post_types as $pt_slug => $pt_name ) { $item_id = 'custom-pt-category-' . $category->term_id . '-' . $pt_slug; $title = $category->name . ' (' . $pt_name . ')'; $url = get_custom_post_type_category_link( $category->term_id, $pt_slug ); ?> <li> <label class="menu-item-title"> <input type="checkbox" class="menu-item-checkbox" name="menu-item[<?php echo $item_id; ?>][menu-item-object-id]" value="<?php echo $item_id; ?>"> <?php echo esc_html( $title ); ?> </label> <input type="hidden" class="menu-item-type" name="menu-item[<?php echo $item_id; ?>][menu-item-type]" value="custom"> <input type="hidden" class="menu-item-title" name="menu-item[<?php echo $item_id; ?>][menu-item-title]" value="<?php echo esc_attr( $title ); ?>"> <input type="hidden" class="menu-item-url" name="menu-item[<?php echo $item_id; ?>][menu-item-url]" value="<?php echo esc_url( $url ); ?>"> </li> <?php } } ?> </ul> </div> <p class="button-controls"> <span class="add-to-menu"> <input type="submit" class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e( '添加到菜单' ); ?>" name="add-custom-post-type-category-menu-item" id="submit-custom-post-type-category"> <span class="spinner"></span> </span> </p> </div> <?php } // 确保自定义菜单项正确保存 function custom_post_type_category_menu_item_save( $menu_id, $menu_item_db_id, $menu_item ) { if ( isset( $menu_item['menu-item-type'] ) && $menu_item['menu-item-type'] === 'custom' ) { return; } } add_action( 'wp_update_nav_menu_item', 'custom_post_type_category_menu_item_save', 10, 3 );
添加代码后,刷新后台菜单页面,你会看到一个新的「分类+自定义文章类型」元框,里面列出了所有分类和你的两个自定义文章类型的组合选项,勾选后点击「添加到菜单」就能直接使用了。
小提示
- 如果你用了方法2的重写规则,一定要更新固定链接设置,否则新URL不会生效。
- 把代码封装成自定义插件会比放到主题
functions.php更稳妥,避免主题更新导致代码丢失。
内容的提问来源于stack exchange,提问作者Michiel Standaert
相关产品推荐
相关产品推荐

