这是因为WordPress现在将您的旧帖子名称读取为类别名称,但它找不到该类别。
解决方案:过滤器404_template
并尝试找到帖子及其永久链接。然后重定向。
<?php # -*- coding: utf-8 -*-
/* Plugin Name: Redirect to category */
add_filter( \'404_template\', \'t5_redirect_to_category\' );
function t5_redirect_to_category( $template )
{
if ( ! is_404() )
return $template;
global $wp_rewrite, $wp_query;
if ( \'/%category%/%postname%/\' !== $wp_rewrite->permalink_structure )
return $template;
if ( ! $post = get_page_by_path( $wp_query->query[\'category_name\'], OBJECT, \'post\' ) )
return $template;
$permalink = get_permalink( $post->ID );
wp_redirect( $permalink, 301 );
exit;
}