admin管理员组文章数量:1334337
Hi I'm looking for code that I can place in my theme's functions.php
that will redirect 404 pages to the previously viewed category.
For example:
If www.example/category1/item-name
goes to a 404, then redirect to www.example/category1/
Edited:
Think I found it:
function __404_template_redirect()
{
if( is_404() )
{
$req = $_SERVER['REQUEST_URI'];
if ( is_file( $req )) {
return; // don't reduce perf by redirecting files to home url
}
// pull the parent directory and convert to site url
$base_dir = dirname( $req );
$parent_url = site_url( $base_dir );
// redirect to parent directory
wp_redirect( $parent_url, 301 );
exit();
}
}
add_action( 'template_redirect', '__404_template_redirect' );
本文标签: functions404 redirect to previous category
版权声明:本文标题:functions - 404 redirect to previous category 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742258522a2442075.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论