Rediriger la page de fichier média vers l’article parent (I)

Word­Press crée auto­ma­ti­que­ment une page pour chaque fichier média uploa­dé dans la biblio­thèque. Ce n’est pas souvent utile et un visi­teur – et Google – peut être amené à visi­ter cette page. Autant les redi­ri­ger vers le billet où le média est affi­ché et c’est l’in­té­rêt de ce code qui est à insé­rer dans le fichier functions.php de votre thème.

source : http://wpsites.net/wordpress-tips/5‑ways-to-redirect-attachment-pages-to-the-parent-post-url/

Code

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();
    endif;
}

If you have found a spel­ling error, please, noti­fy us by selec­ting that text and pres­sing Ctrl+Enter.

Ce site embarque des cookies pour vous offrir la meilleur expérience possible

Spelling error report

The following text will be sent to our editors: