Last active
August 17, 2017 09:05
-
-
Save braddalton/eea4f721388c6b38248c to your computer and use it in GitHub Desktop.
Redirect Attachment Pages To The Parent Post URL http://wpsites.net/wordpress-tips/5-ways-to-redirect-attachment-pages-to-the-parent-post-url/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'template_redirect', 'wpsites_attachment_redirect' ); | |
function wpsites_attachment_redirect(){ | |
global $post; | |
if ( is_attachment() && isset($post->post_parent) && is_numeric($post->post_parent) && ($post->post_parent != 0) ) : | |
wp_redirect( get_permalink( $post->post_parent ), 301 ); | |
exit(); | |
wp_reset_postdata(); | |
endif; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 7 is unreachable because of the
exit();
Also is there any need to reset the post data in this instance?