permalinks - Redirect %year%%monthnum%%day%%postname%

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add

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 question

Need 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 question

Need 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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

I 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745270495a4619714.html

相关推荐

  • permalinks - Redirect %year%%monthnum%%day%%postname%

    Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信