求助:如何在Woocommerce我的账户导航菜单中添加副标题
Hey there! Let's solve this problem of adding subtitles under each menu item in your WooCommerce My Account section. Here are two reliable, update-safe approaches:
Method 1: Custom Function + Template Override (Recommended)
This method keeps your changes protected from theme/WooCommerce updates and avoids modifying core plugin files.
Step 1: Copy the WooCommerce Template to Your Theme
First, copy the navigation.php file from wp-content/plugins/woocommerce/templates/myaccount/ into your theme's directory at wp-content/themes/your-theme/woocommerce/myaccount/. This way, your customizations won't get overwritten when you update WooCommerce.
Step 2: Define Subtitles with a Custom Function
Add this code to your theme's functions.php file (or a custom plugin):
// Define subtitles for each My Account menu item function get_my_account_menu_subtitles() { return array( 'dashboard' => 'View your account overview', 'orders' => 'Track & manage purchases', 'downloads' => 'Access digital products', 'edit-address' => 'Update shipping/billing info', 'edit-account' => 'Change personal details', 'customer-logout' => 'Sign out securely' ); }
Tweak the subtitle text or add/remove entries to match your specific menu items.
Step 3: Modify the Template to Display Subtitles
Open the copied navigation.php file and update the loop to include the subtitle:
<?php // Fetch our custom subtitle array $menu_subtitles = get_my_account_menu_subtitles(); ?> <?php foreach ( wc_get_account_menu_items() as $endpoint => $label ) : ?> <li class="<?php echo wc_get_account_menu_item_classes( $endpoint ); ?>"> <a href="<?php echo esc_url( wc_get_account_endpoint_url( $endpoint ) ); ?>"> <?php echo esc_html( $label ); ?> <!-- Show subtitle if it exists for this menu item --> <?php if ( isset( $menu_subtitles[ $endpoint ] ) ) : ?> <span class="account-menu-subtitle"><?php echo esc_html( $menu_subtitles[ $endpoint ] ); ?></span> <?php endif; ?> </a> </li> <?php endforeach; ?>
Step 4: Style the Subtitle (Optional)
Add this CSS to your theme's custom CSS section or style.css to make the subtitle look polished:
.account-menu-subtitle { display: block; font-size: 0.85em; color: #767676; margin-top: 3px; font-weight: normal; }
Method 2: Attach Subtitles via Filter Hook
If you prefer to pass subtitle data directly through WooCommerce's menu filter, use this approach (just be mindful it doesn't conflict with other plugins):
Step 1: Add Filter to Attach Subtitles
Add this to your functions.php:
add_filter( 'woocommerce_account_menu_items', 'attach_subtitles_to_account_menu' ); function attach_subtitles_to_account_menu( $items ) { $updated_items = array(); foreach ( $items as $endpoint => $label ) { // Assign subtitles based on menu endpoint $subtitle = match( $endpoint ) { 'dashboard' => 'Your account at a glance', 'orders' => 'See all past orders', 'downloads' => 'Get your digital files', 'edit-address' => 'Update your locations', 'edit-account' => 'Manage profile & password', 'customer-logout' => 'Sign out of your account', default => '' }; // Store label and subtitle as an array $updated_items[ $endpoint ] = array( 'label' => $label, 'subtitle' => $subtitle ); } return $updated_items; }
Step 2: Update the Template to Use the Array
Modify your copied navigation.php loop to read from the array:
<?php foreach ( wc_get_account_menu_items() as $endpoint => $item ) : ?> <li class="<?php echo wc_get_account_menu_item_classes( $endpoint ); ?>"> <a href="<?php echo esc_url( wc_get_account_endpoint_url( $endpoint ) ); ?>"> <?php echo esc_html( $item['label'] ); ?> <?php if ( ! empty( $item['subtitle'] ) ) : ?> <span class="account-menu-subtitle"><?php echo esc_html( $item['subtitle'] ); ?></span> <?php endif; ?> </a> </li> <?php endforeach; ?>
Both methods will let you display subtitles exactly where you want them — right under the main menu label inside the <li> element. The first method is more lightweight and less likely to cause conflicts with other tools or updates.
内容的提问来源于stack exchange,提问作者lisarko8077

