我想问你一些我真正困惑要解决的问题。我的wordpress网站有问题,它有重复的网址,结果相同。
当我修改主题功能时,我得到了数字2:
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?
谢谢。
答案 0 :(得分:1)
我得到了解决方案,在这种情况下,我在我的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;
}
}
}