最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论
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.

发布评论

评论列表(0)

  1. 暂无评论