如何为WordPress多站点各子站配置移动端头部自定义锚点按钮
Absolutely feasible! You can easily make that mobile "GET STARTED" button display site-specific text and link to the correct slug for each subdirectory multilingual site. Here are a few practical methods to implement this:
Method 1: Use WordPress Multisite's Blog ID
This approach relies on identifying each site by its unique blog ID, which works great if you have a small number of sites:
- First, find the ID for each site in your network: Go to My Sites > Network Admin > Sites – the ID is listed in each site's row.
- Replace your existing anchor tag in
header.phpwith this code:<?php // Define CTA settings for each site (match ID to your network's site IDs) $cta_configs = array( 1 => array( // Main site (e.g., yourdomain.com) 'text' => 'GET STARTED', 'slug' => '/subscribe/' ), 2 => array( // German site (yourdomain.com/de) 'text' => 'LOS GEHEN', 'slug' => '/anmelden/' ), 3 => array( // UK site (yourdomain.com/uk) 'text' => 'GET STARTED', 'slug' => '/subscribe/' ) // Add more entries for additional sites ); // Get current site's config, fall back to main site if not found $current_blog_id = get_current_blog_id(); $cta = isset($cta_configs[$current_blog_id]) ? $cta_configs[$current_blog_id] : $cta_configs[1]; ?> <a class="button_cta_mobile" href="<?php echo home_url() . $cta['slug']; ?>"><?php echo esc_html($cta['text']); ?></a>
Method 2: Leverage Multilingual Plugin Functions (if using Polylang/WPML)
If you're using a multilingual plugin like Polylang or WPML, you can use their built-in functions to target language slugs directly (which match your subdirectory structure):
Example with Polylang:
<?php // Get current language slug (matches your subdirectory: 'de', 'uk', etc.) $current_lang = pll_current_language('slug'); // Define CTA settings per language $cta_configs = array( 'en' => array( 'text' => 'GET STARTED', 'slug' => '/subscribe/' ), 'de' => array( 'text' => 'LOS GEHEN', 'slug' => '/anmelden/' ), 'uk' => array( 'text' => 'ПОЧАТИ', 'slug' => '/підписатися/' ) ); // Get current language's config, fall back to English $cta = isset($cta_configs[$current_lang]) ? $cta_configs[$current_lang] : $cta_configs['en']; ?> <a class="button_cta_mobile" href="<?php echo home_url() . $cta['slug']; ?>"><?php echo esc_html($cta['text']); ?></a>
Example with WPML:
Use icl_get_current_language() instead of pll_current_language() to get the current language code, then structure the config array the same way.
Method 3: Custom Site Settings (No Code Changes After Setup)
For the most flexibility (great if non-technical admins manage individual sites), add custom settings to each site's WordPress admin so you can edit the CTA text and slug without touching code:
Add this code to your network-wide
functions.php(or a custom plugin):function add_mobile_cta_settings() { // Add settings section to General Settings page add_settings_section( 'mobile_cta_section', 'Mobile Header CTA Button', function() { echo '<p>Set the text and target URL slug for the mobile header CTA button.</p>'; }, 'general' ); // CTA Text field add_settings_field( 'mobile_cta_text', 'CTA Button Text', function() { $text = get_option('mobile_cta_text', 'GET STARTED'); echo '<input type="text" name="mobile_cta_text" value="' . esc_attr($text) . '" class="regular-text">'; }, 'general', 'mobile_cta_section' ); // CTA Slug field add_settings_field( 'mobile_cta_slug', 'CTA Target Slug (e.g., /subscribe/)', function() { $slug = get_option('mobile_cta_slug', '/subscribe/'); echo '<input type="text" name="mobile_cta_slug" value="' . esc_attr($slug) . '" class="regular-text">'; }, 'general', 'mobile_cta_section' ); // Register settings to save values register_setting('general', 'mobile_cta_text'); register_setting('general', 'mobile_cta_slug'); } add_action('admin_init', 'add_mobile_cta_settings');Update your
header.phpcode to pull these settings:<?php $cta_text = get_option('mobile_cta_text', 'GET STARTED'); $cta_slug = get_option('mobile_cta_slug', '/subscribe/'); ?> <a class="button_cta_mobile" href="<?php echo home_url() . esc_attr($cta_slug); ?>"><?php echo esc_html($cta_text); ?></a>
Now, each site's admin can go to Settings > General and edit the CTA text and slug directly from the dashboard!
内容的提问来源于stack exchange,提问作者Eivindur

