• Hi

    I need to redirect attachment pages (images) to there parent post url (products), but if the images is uploadet directly to the image library and then afterwards attached in the product, then $post->post_parent will return 0
    Is there a way to do it anyway?

    5 Ways To Redirect Attachment Pages To The Parent Post URL

    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;
    }

    If this cant be done – can someone please suggest how i can remove all the attachment pages from google? then i will have to redirect all homepage.

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘redirect attachment pages to parent’ is closed to new replies.