求助:如何在WordPress中为Tagline添加自定义URL链接
Got it! Let's walk through how to make your site's tagline link to a custom URL by modifying your child theme's functions.php file. I'll structure this to match the pattern you used for your logo URL, plus add flexible options for easier long-term management.
Basic Implementation (Direct Code Edit)
First, we'll override the default tagline output to wrap it in a clickable link. Add this code to the bottom of your child theme's functions.php file:
// Remove WordPress core's default tagline output remove_action( 'wp_site_tagline', 'wp_site_tagline' ); // Add our custom linked tagline add_action( 'wp_site_tagline', 'custom_linked_site_tagline' ); function custom_linked_site_tagline() { // Replace this with your desired target URL $tagline_link = 'https://your-custom-destination.com'; // Grab your existing tagline text from site settings $tagline_text = get_bloginfo( 'description' ); // Only output the tagline if it exists in your settings if ( ! empty( $tagline_text ) ) { // Output linked tagline with security escaping to prevent vulnerabilities echo '<p class="site-description"> <a href="' . esc_url( $tagline_link ) . '" title="' . esc_attr( $tagline_text ) . '"> ' . esc_html( $tagline_text ) . ' </a> </p>'; } }
Quick Notes:
- Security Matters: We use
esc_url(),esc_attr(), andesc_html()to sanitize all output—this stops cross-site scripting (XSS) risks, which is non-negotiable for safe WordPress code. - Theme Hook Check: If your theme uses a custom hook (instead of the core
wp_site_taglineaction, e.g.,mytheme_output_tagline), swap outwp_site_taglinewith your theme's specific hook name in bothremove_actionandadd_action.
Advanced: Add a Customizer Setting (No More Code Edits)
If you want to manage the tagline URL from the WordPress Customizer (instead of editing code every time), add this code alongside the above:
// Add a Tagline URL field to the Customizer's Site Identity section add_action( 'customize_register', 'custom_tagline_url_customizer' ); function custom_tagline_url_customizer( $wp_customize ) { // Create a setting to store our custom URL $wp_customize->add_setting( 'tagline_custom_url', array( 'default' => '', 'sanitize_callback' => 'esc_url_raw', // Automatically clean the input URL 'transport' => 'refresh', ) ); // Add the visible control for users to input the URL $wp_customize->add_control( 'tagline_custom_url', array( 'label' => __( 'Tagline Link URL', 'your-child-theme-slug' ), 'section' => 'title_tagline', 'type' => 'url', 'priority' => 15, // Places it right after the default tagline field ) ); }
Then update the custom_linked_site_tagline() function to pull the URL from the Customizer instead of hardcoding it:
function custom_linked_site_tagline() { // Get the URL from the Customizer (fallback to empty if not set) $tagline_link = get_theme_mod( 'tagline_custom_url', '' ); $tagline_text = get_bloginfo( 'description' ); if ( ! empty( $tagline_text ) ) { if ( ! empty( $tagline_link ) ) { // Output linked tagline if a URL is set echo '<p class="site-description"> <a href="' . esc_url( $tagline_link ) . '" title="' . esc_attr( $tagline_text ) . '"> ' . esc_html( $tagline_text ) . ' </a> </p>'; } else { // Fallback to the default unlinked tagline if no URL is provided echo '<p class="site-description">' . esc_html( $tagline_text ) . '</p>'; } } }
Now you can head to Appearance > Customize > Site Identity to set or update your tagline's URL anytime—no code edits required!
Troubleshooting Tips
- White Screen? If your site breaks after adding code, use FTP to access your child theme's
functions.phpand remove the problematic code. This usually happens from a syntax error (like a missing semicolon or bracket). - Tagline Not Updating? Clear your site cache (if using a caching plugin) and refresh the front end.
- Hardcoded Tagline in Theme? If your theme outputs the tagline directly in
header.php(instead of using hooks), copyheader.phpfrom the parent theme to your child theme, then manually wrap the tagline text in an<a>tag.
内容的提问来源于stack exchange,提问作者Gerri Jensen

