AMP文章中重复canonical链接与标题标签的移除方法求助
Hey there, let’s work through this duplicate canonical and title tag issue in your AMP posts—this is a super common conflict between SEO plugins and AMP tools, so I’ve got a few targeted fixes to try:
Before jumping into fixes, you need to know which component is doubling up on tags. Here’s how:
- Open one of your AMP posts in a browser, right-click > View Page Source.
- Search for
rel="canonical"and<title>—look for duplicate entries, and note any differences in the URL or text (this can hint at whether it’s your SEO plugin, AMP plugin, or theme adding the extra tag). - For WordPress sites, you can also use the Query Monitor plugin to see which hooks are firing to add these tags (look for hooks like
wp_headoramp_post_template_head).
If your previous hook attempts failed, it’s almost certainly a priority issue. SEO plugins often use very early hook priorities (like 1) to inject their tags, while default hooks run at 10. To override this, use a lower priority number to remove the duplicate before it loads, or a higher number to clean up after:
Example for Removing a Duplicate Canonical (Yoast SEO + AMP Plugin):
add_action( 'wp_head', function() { // Remove Yoast's canonical from AMP pages if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) { remove_action( 'wp_head', 'wpseo_canonical', 1 ); } }, 0 ); // Runs before Yoast's hook (priority 1)
Example for Removing a Duplicate Title (Rank Math + AMP Plugin):
add_action( 'amp_post_template_head', function() { // Remove Rank Math's title from AMP template head remove_action( 'amp_post_template_head', 'rank_math/frontend/title', 5 ); }, 1 ); // Runs before Rank Math's AMP hook
Pro tip: Use your SEO plugin’s documentation to find the exact function name and priority it uses for AMP tags.
AMP pages don’t always respect standard wp_head hooks—they use their own template hooks like amp_post_template_head. If you were using global hooks before, switch to AMP-specific ones to target only AMP content:
// Remove duplicate canonical from AMP template head add_action( 'amp_post_template_head', function() { // Replace with your SEO plugin's canonical function remove_action( 'amp_post_template_head', 'your_seo_plugin_amp_canonical', 5 ); }, 1 );
Most SEO plugins (Yoast, Rank Math, etc.) have built-in AMP support that can clash with standalone AMP plugins. Try this:
- Go to your SEO plugin’s settings, find the AMP tab, and disable its AMP integration.
- Let your standalone AMP plugin handle the AMP markup, and configure it to pull canonical/title data from your SEO plugin (look for settings like "Use SEO Plugin Metadata").
If all else fails, you can directly edit the AMP page’s HTML to remove duplicates. Use a filter to scan and clean up the head content:
Safe DOM-Based Approach (Avoids Regex Errors):
add_filter( 'amp_post_template_head', function( $head_content ) { libxml_use_internal_errors( true ); // Suppress HTML parsing warnings $dom = new DOMDocument(); $dom->loadHTML( '<html><head>' . $head_content . '</head></html>' ); libxml_clear_errors(); // Remove extra canonical links (keep only the first one) $canonicals = $dom->getElementsByTagName( 'link' ); $keep_canonical = true; for ( $i = $canonicals->length - 1; $i >= 0; $i-- ) { $link = $canonicals->item( $i ); if ( $link->getAttribute( 'rel' ) === 'canonical' ) { if ( !$keep_canonical ) { $link->parentNode->removeChild( $link ); } $keep_canonical = false; } } // Remove extra title tags (keep only the first one) $titles = $dom->getElementsByTagName( 'title' ); $keep_title = true; for ( $i = $titles->length - 1; $i >= 0; $i-- ) { $title = $titles->item( $i ); if ( !$keep_title ) { $title->parentNode->removeChild( $title ); } $keep_title = false; } return str_replace( array( '<html><head>', '</head></html>' ), '', $dom->saveHTML() ); }, 9999 ); // Run after all other head content is added
Some themes inject their own canonical/title tags regardless of AMP. Temporarily switch to a default WordPress theme (like Twenty Twenty-Four) and check if the duplicates disappear. If they do, add a similar hook to remove your theme’s AMP-unfriendly tags.
After trying any fix, use Google’s AMP validation tool to verify your page is valid and free of duplicate tags.
内容的提问来源于stack exchange,提问作者Hansjörg Leichsenring

