Closed. This question needs details or clarity. It is not currently accepting answers.admin管理员组文章数量:1417687
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this questionNeed to redirect to new structure /%year%/%monthnum%/%day%/%category%/%postname%
Saw code that helped before but can't seem to find it. Updated theme and lost the code that went into functions.php. Backup best practices are important!
UPDATE: Found the code again! It was first posted @ Catch 404 after changing permalink structure from /%postname%/ to /%category%/%postname%/
add_filter( '404_template', 't5_redirect_to_category' );
function t5_redirect_to_category( $template )
{
if ( ! is_404() )
return $template;
global $wp_rewrite, $wp_query;
// change 'yourpermalink' to your new permalink structure
// my new structure is '/%year%/%monthnum%/%day%/%category%/%postname%'
if ( 'yourpermalink' !== $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;
}
Stick this code in functions.php using the theme editor. The code can redirect all new permalink configurations that are currently returning as a 404 due to changes.
Remember to save this code because every time you update the theme it will be removed and you will need to add it to functions.php again.
This literally saved me. Thanks !
Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this questionNeed to redirect to new structure /%year%/%monthnum%/%day%/%category%/%postname%
Saw code that helped before but can't seem to find it. Updated theme and lost the code that went into functions.php. Backup best practices are important!
UPDATE: Found the code again! It was first posted @ Catch 404 after changing permalink structure from /%postname%/ to /%category%/%postname%/
add_filter( '404_template', 't5_redirect_to_category' );
function t5_redirect_to_category( $template )
{
if ( ! is_404() )
return $template;
global $wp_rewrite, $wp_query;
// change 'yourpermalink' to your new permalink structure
// my new structure is '/%year%/%monthnum%/%day%/%category%/%postname%'
if ( 'yourpermalink' !== $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;
}
Stick this code in functions.php using the theme editor. The code can redirect all new permalink configurations that are currently returning as a 404 due to changes.
Remember to save this code because every time you update the theme it will be removed and you will need to add it to functions.php again.
This literally saved me. Thanks https://wordpress.stackexchange/users/73/fuxia!
Share Improve this question edited Aug 4, 2019 at 17:32 Postard asked Aug 3, 2019 at 19:30 PostardPostard 113 bronze badges1 Answer
Reset to default 0I can only tell you how I handle things like this.
I swear by redirections which is a plugin that handles this sort of thing. It can be set to detect permalink changes and redirect old to new. Then you just go into perma-link and restructure as you wish. As someone who tends to figure out better ways to do things as he works, this is so much help for me.
本文标签: permalinksRedirect yearmonthnumdaypostname
版权声明:本文标题:permalinks - Redirect %year%%monthnum%%day%%postname% 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745270494a2650859.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论