admin管理员组文章数量:1122832
I have a category archive page at:
It displays an archive of items from the category 'news-article'
I'd like to redirect any requests for to (so that the former is never directly accessible).
Is there a best practice? Should I put a 301 Redirect in my .htaccess file (or use a plugin to do the same)?
Or should I use wp_safe_redirect
? If yes, which action hook should I use? As in:
add_action( 'WHICH_ACTION_HOOK??', 'adam_redirect_news' );
function adam_redirect_news () {
if ( is_category( 'news-article' ) ) {
wp_safe_redirect( '' );
exit;
}
}
I have a category archive page at: http://mysite.com/news
It displays an archive of items from the category 'news-article'
I'd like to redirect any requests for http://mysite.com/category/news-article to http://mysite.com/news (so that the former is never directly accessible).
Is there a best practice? Should I put a 301 Redirect in my .htaccess file (or use a plugin to do the same)?
Or should I use wp_safe_redirect
? If yes, which action hook should I use? As in:
add_action( 'WHICH_ACTION_HOOK??', 'adam_redirect_news' );
function adam_redirect_news () {
if ( is_category( 'news-article' ) ) {
wp_safe_redirect( 'http://mysite.com/news' );
exit;
}
}
Share
Improve this question
asked Sep 9, 2014 at 1:45
Pat GilmourPat Gilmour
6122 gold badges5 silver badges16 bronze badges
4 Answers
Reset to default 9I don't know for some reason, the add_filter caused error. I used the following one:
function my_page_template_redirect()
{
if ( is_category( 'news-articles' ) ) {
$url = site_url( '/news' );
wp_safe_redirect( $url, 301 );
exit();
}
}
add_action( 'template_redirect', 'my_page_template_redirect' );
Thanks to @Layka above, I tweaked the code slightly and this does what I needed.
It could perhaps be any filter called around the same time - not sure.
/**
* Redirect 'category/news-articles' category to 'News page' ( at http://www.example.com/news' )
*
*/
add_filter('template_redirect', 'template_redirect_filter', 10, 3);
function template_redirect_filter( $url, $term, $taxonomy ) {
if ( is_category( 'news-articles' ) ) {
$url = site_url( '/news' );
wp_safe_redirect( $url, 301 );
exit;
}
return $url;
}
function template_category_template_redirect()
{
if( is_category())
{
wp_redirect( site_url() );
die;
}
}
add_action( 'template_redirect','template_category_template_redirect' );
Another possible way is to have a custom template in your Child-Theme. According to the wordpress template hierarchy the file must be named category-news-article.php.
In this file you could do a redirect with wp_safe_redirect. e.g.
<?php
wp_safe_redirect('/news', 301, '');
With this approach only the category news-article will be redirected. All other category keep working as before. This might come in handy if you want to point each category to a different url. It also works with custom post type as well if your named the template according to the wordpress hierarchy (see link above).
本文标签: categoriesBest Way to Redirect Category to Page and Hide Category
版权声明:本文标题:categories - Best Way to Redirect Category to Page and Hide Category 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736301605a1931236.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论