将Wordpress Media附件页面重定向到根位置 [英] Redirect Wordpress Media attachment page to root location

查看:71
本文介绍了将Wordpress Media附件页面重定向到根位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前的网站结构以前是 example.com/wp .我已移至 example.com

My old website structure used to be example.com/wp. I have moved to example.com

所以现在我正尝试使用 wp_redirect 重定向301我的所有媒体附件页面(超过1500页),如下所示:

SO now I am trying to use wp_redirect to redirect 301 all my media attachment pages(over 1500 pages) like this:

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

我正在使用此挂钩

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

想法是制作 wp_redirect(example.com/>此处的任何附件链接< ;, 301);

推荐答案

比我想象的要容易,这是解决方法:

It was easier than I thought and here is the solution:

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

这篇关于将Wordpress Media附件页面重定向到根位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆