Php 重定向Wordpress自定义永久链接中的重复URL
我想问你一些我很困惑的问题。我的wordpress网站有一个问题,它复制了相同结果的URLPhp 重定向Wordpress自定义永久链接中的重复URL,php,wordpress,.htaccess,permalinks,Php,Wordpress,.htaccess,Permalinks,我想问你一些我很困惑的问题。我的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', a
add_action( 'init', 'add_news_slug_permalink', 1 );
function add_news_slug_permalink() {
register_post_type( 'post', array(
'rewrite' => array( 'slug' => 'news' ),
) );
}
问题是,如何在没有news
slug的情况下重定向或禁用URL到带有news
slug的URL?
谢谢 我得到了解决方案,在本例中,我在
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您能给我一些示例吗?