2017-09-05 9 views
0

私の古いウェブサイトの構造はexample.com/wpでした。私はこのフックにWordpressメディアの添付ファイルのページをルートの場所にリダイレクト

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; 
} 
を使用しています

example.com/wp/image1 ====> example.com/image1 

:私は今、私はこのように(1500ページ以上)301すべての私のメディアの添付ファイルのページをリダイレクトするwp_redirectを使用しようとしていますexample.com

に移動しました

そして、アイデアがwp_redirect(example.com/>Any attachment link here<, 301);

答えて

0

思ったよりも簡単だったし、ここでのソリューションです:

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)) : 

    $actual_link = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; 
    $actual_link = $actual_link.'/'.$post->post_parent; 
    $actual_link = str_replace('/wp/','/',$actual_link); 
    wp_redirect($actual_link, 301); 
    exit(); 
    wp_reset_postdata(); 
    endif; 
} 
0

image.phpファイルを作成することです

コードをファイルに貼り付け、テーマのルートディレクトリにアップロードします。

<?php wp_redirect(get_permalink($post->post_parent), 301); exit; ?> 
+0

問題は、私は永遠に私を取るようにリダイレクトするために1500以上の添付ファイルを持っているということです。フックがいいだろう。 –

+0

Mrjack @hopeこれはあなたを助けます –

0

後、親のないメディアの添付ファイルのページと互換性のある代替ファイルimage.php! (それは他の二人の答えとミックスです)

<?php 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; ?> 

<?php wp_redirect(get_bloginfo('wpurl'), 301); exit; ?> 
関連する問題