在Wordpress中重定向重复的URL自定义固定链接
问题描述:
我想问你一些我真正混淆解决的问题。我的WordPress网站有问题,它的网址重复,结果相同。在Wordpress中重定向重复的URL自定义固定链接
- www.site.com/some-blog-post
- www.site.com/news/some-blog-post
我拿到了2号,当我修改我的主题功能:
add_action('init', 'add_news_slug_permalink', 1);
function add_news_slug_permalink() {
register_post_type('post', array(
'rewrite' => array('slug' => 'news' ),
));
}
的问题是,如何重定向或禁用URL不news
塞与news
塞至网址是什么? 谢谢。
答
我得到了解决,在这种情况下,我在function.php
添加一个动作,就好像:
add_action('template_redirect', 'redirect_to_news_slug', 20);
function redirect_to_news_slug() {
global $post;
/**
* Check if the page is single Post
*/
if ($post->post_type == 'post' && (is_category() == false) &&
(is_single() == true) && (is_home() == false)) {
$currentUrl = $_SERVER["REQUEST_URI"];
/**
* Remove slash from URL, and get the first slug
*/
$partOfUrl = array_filter(explode('/', $currentUrl));
/**
* Check if URL doesn't have 'news'
*/
if ($partOfUrl[1] !== 'news') {
/**
* set new URL with 'news' slug
*/
$newUrl = home_url().'/news/' . $partOfUrl[1];
/**
* Redirect to new URL
*/
wp_redirect($newUrl,301);
exit;
}
}
}
最坏的情况下,你很可能通过修改.htaccess文件做到这一点。添加一个重写条件。 – Rasclatt
@Rasclatt你可以给我任何样品吗? –