2017-09-05 6 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 

: 나는 (301) 내 모든 미디어 첨부 페이지 같은 (1500 페이지) 리디렉션 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

(그것은 다른 두 사람의 답변 혼합이다). 후크가 좋을 것입니다. –

+0

Mrjack @hope이 도움이 될 것입니다. –

0

후 부모없이 미디어 첨부 페이지와 호환되는 다른 파일 image.php! 이 문제는 내가 그래서 영원히 저를 취할 것입니다 리디렉션 1500 첨부 파일을 가지고있다

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