在自定义WordPress页面添加读取自定义菜单的select下拉选择框
Got it, let's walk through how to replace your test text field with a dropdown that lists all your custom WordPress menus. This is straightforward once you know the right functions to use—here's a step-by-step solution:
1. Fetch All Custom WordPress Menus
First, we'll use WordPress's built-in wp_get_nav_menus() function to pull all the custom menus you've created. This returns an array of WP_Term objects, each containing the menu's ID and name.
2. Render the Select Dropdown Field
Replace your existing text input with this select field. We'll loop through the fetched menus to generate options, and add logic to keep the selected menu highlighted if you've already saved a choice.
<?php // Retrieve all custom navigation menus $all_menus = wp_get_nav_menus(); // Get the previously saved menu ID (adjust the meta key to match your setup) $selected_menu = get_post_meta( get_the_ID(), '_user_selected_menu', true ); ?> <!-- Add this to your custom page's form --> <div class="form-group"> <label for="custom-menu-select">Choose a Custom Menu:</label> <?php // Add nonce for security (critical for backend submissions) ?> <?php wp_nonce_field( 'menu_selection_nonce', 'menu_nonce' ); ?> <select id="custom-menu-select" name="user_selected_menu" class="regular-text"> <option value="">-- Select a Menu --</option> <?php foreach ( $all_menus as $menu ) : ?> <option value="<?php echo esc_attr( $menu->term_id ); ?>" <?php selected( $selected_menu, $menu->term_id ); ?> > <?php echo esc_html( $menu->name ); ?> </option> <?php endforeach; ?> </select> </div>
3. Save the Selected Menu (Optional)
If you need to store the user's selection, add this save logic using the save_post hook. This will save the chosen menu ID as post meta (adjust the post type if your custom page isn't a standard post).
add_action( 'save_post', 'save_user_selected_menu', 10, 1 ); function save_user_selected_menu( $post_id ) { // Skip auto-saves and ensure user has edit permissions if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; if ( ! current_user_can( 'edit_post', $post_id ) ) return; // Validate nonce for security if ( ! isset( $_POST['menu_nonce'] ) || ! wp_verify_nonce( $_POST['menu_nonce'], 'menu_selection_nonce' ) ) { return; } // Save the selected menu ID if it exists if ( isset( $_POST['user_selected_menu'] ) ) { update_post_meta( $post_id, '_user_selected_menu', sanitize_text_field( $_POST['user_selected_menu'] ) ); } else { // Clear the meta if no menu was selected delete_post_meta( $post_id, '_user_selected_menu' ); } }
Key Notes:
- Security: Always use
esc_attr(),esc_html(), andsanitize_text_field()to prevent XSS vulnerabilities. The nonce field is critical to verify that the form submission is legitimate. - Meta Key: Adjust
_user_selected_menuto a unique meta key that fits your project (avoid generic names to prevent conflicts). - Custom Post Types: If your custom page uses a custom post type, add a check in the
save_user_selected_menufunction to target only that post type (e.g.,if ( get_post_type( $post_id ) !== 'your_custom_type' ) return;).
内容的提问来源于stack exchange,提问作者Junior

