Title: BSG21's Replies | WordPress.org

---

# BSG21

  [  ](https://wordpress.org/support/users/bsg21/)

 *   [Profile](https://wordpress.org/support/users/bsg21/)
 *   [Topics Started](https://wordpress.org/support/users/bsg21/topics/)
 *   [Replies Created](https://wordpress.org/support/users/bsg21/replies/)
 *   [Reviews Written](https://wordpress.org/support/users/bsg21/reviews/)
 *   [Topics Replied To](https://wordpress.org/support/users/bsg21/replied-to/)
 *   [Engagements](https://wordpress.org/support/users/bsg21/engagements/)
 *   [Favorites](https://wordpress.org/support/users/bsg21/favorites/)

 Search replies:

## Forum Replies Created

Viewing 6 replies - 1 through 6 (of 6 total)

 *   Forum: [Plugins](https://wordpress.org/support/forum/plugins-and-hacks/)
    In
   reply to: [[Yoast SEO - Advanced SEO with real-time guidance and built-in AI] Sitemap Question](https://wordpress.org/support/topic/sitemap-question-3/)
 *  Thread Starter [BSG21](https://wordpress.org/support/users/bsg21/)
 * (@bsg21)
 * [15 years ago](https://wordpress.org/support/topic/sitemap-question-3/#post-2100428)
 * Thanks for that. Just wanted to make sure. Been doing a lot of auditing lately
   and that jumped out at me.
 *   Forum: [Plugins](https://wordpress.org/support/forum/plugins-and-hacks/)
    In
   reply to: [[Yoast SEO - Advanced SEO with real-time guidance and built-in AI] Sitemap Question](https://wordpress.org/support/topic/sitemap-question-3/)
 *  Thread Starter [BSG21](https://wordpress.org/support/users/bsg21/)
 * (@bsg21)
 * [15 years ago](https://wordpress.org/support/topic/sitemap-question-3/#post-2100257)
 * Duplicate
 *   Forum: [Plugins](https://wordpress.org/support/forum/plugins-and-hacks/)
    In
   reply to: [[Yoast SEO - Advanced SEO with real-time guidance and built-in AI] Ability to edit Canonical for attachment.php](https://wordpress.org/support/topic/ability-to-edit-canonical-for-attachmentphp/)
 *  Thread Starter [BSG21](https://wordpress.org/support/users/bsg21/)
 * (@bsg21)
 * [15 years ago](https://wordpress.org/support/topic/ability-to-edit-canonical-for-attachmentphp/#post-2091630)
 * Awesome, that works perfectly. Thank you!
 *   Forum: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
   
   In reply to: [Getting Parent Post Outside of Loop](https://wordpress.org/support/topic/getting-parent-post-outside-of-loop/)
 *  Thread Starter [BSG21](https://wordpress.org/support/users/bsg21/)
 * (@bsg21)
 * [15 years ago](https://wordpress.org/support/topic/getting-parent-post-outside-of-loop/#post-2097296)
 * This code will do it:
 *     ```
       global $wp_query;
        $attachment_page_id = $wp_query->post->ID;
        $parent_post = get_post($attachment_page_id);
        $parent_post_id = $parent_post->post_parent;
   
        $parent_post_permalink = get_permalink($parent_post_id);
       ```
   
 *   Forum: [Plugins](https://wordpress.org/support/forum/plugins-and-hacks/)
    In
   reply to: [[Yoast SEO - Advanced SEO with real-time guidance and built-in AI] Ability to edit Canonical for attachment.php](https://wordpress.org/support/topic/ability-to-edit-canonical-for-attachmentphp/)
 *  Thread Starter [BSG21](https://wordpress.org/support/users/bsg21/)
 * (@bsg21)
 * [15 years ago](https://wordpress.org/support/topic/ability-to-edit-canonical-for-attachmentphp/#post-2091617)
 * Alright, I got it to work but it is outputting the canonical <link > tag twice.
 * This is the code I used I just need to know what extra step(s) I need to take
   to keep it from duplicating
 *     ```
       function attachment_wpseo_canonical ($can) {
        global $wp_query;
        $attachment_page_id = $wp_query->post->ID;
        $parent_post = get_post($attachment_page_id);
        $parent_post_id = $parent_post->post_parent;
   
        $parent_post_permalink = get_permalink($parent_post_id);
        echo "<link rel=\"canonical\" href=\"$parent_post_permalink\" />";
        }
        add_filter('wpseo_canonical','attachment_wpseo_canonical');
       ```
   
 *   Forum: [Plugins](https://wordpress.org/support/forum/plugins-and-hacks/)
    In
   reply to: [[Yoast SEO - Advanced SEO with real-time guidance and built-in AI] Ability to edit Canonical for attachment.php](https://wordpress.org/support/topic/ability-to-edit-canonical-for-attachmentphp/)
 *  Thread Starter [BSG21](https://wordpress.org/support/users/bsg21/)
 * (@bsg21)
 * [15 years ago](https://wordpress.org/support/topic/ability-to-edit-canonical-for-attachmentphp/#post-2091597)
 * Thanks for the response. How do I get the post url that the attachment is assigned
   to outside the loop since I will need to do this before the loop.

Viewing 6 replies - 1 through 6 (of 6 total)