将自定义分类术语存档重定向到页面
我希望将我的条款模板重定向到带有保存段的页面(以允许客户端自定义页面并使用此页面的模板模型),这是我的代码,
function redirect_archive_term() {
$categories = get_terms( array('taxonomy' => 'chapters') );
foreach($categories as $category) {
if( is_post_type_archive( $category->slug ) ) { // like chapter-1, chapter-2 ...
wp_redirect( home_url( '/'.$category->slug ), 301 );
exit();
}
}
}
add_action( 'template_redirect', 'redirect_archive_term' );
由于我认为的条件,这不起作用一种针对他们的方法?
感谢您的帮助:)
I wish redirect my terms template to a page with the save slug (to allow the client to custom the page and use a template model for this page), this is my code
function redirect_archive_term() {
$categories = get_terms( array('taxonomy' => 'chapters') );
foreach($categories as $category) {
if( is_post_type_archive( $category->slug ) ) { // like chapter-1, chapter-2 ...
wp_redirect( home_url( '/'.$category->slug ), 301 );
exit();
}
}
}
add_action( 'template_redirect', 'redirect_archive_term' );
this is doesn't work because of the condition I think, there a way to target them ?
Thanks for the help :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应该很简单。以下未经测试。
函数必须在部署标头之前触发,因此使用
wp
钩子。
is_tax()
可以在搜索或存档页面上触发,我们需要将其过滤掉,因此! is_search() && ! is_archive()。
我们从
get_term()
获取当前术语,并且我们通过wp_safe_redirect()
。Should be pretty straight forward. The following is untested.
Function has to fire before the header is deployed, thus the use of the
wp
hook.is_tax()
can fire on search or archive page, we need to filter that out, thus! is_search() && ! is_archive()
.We get the current term from
get_term()
and we redirect throughwp_safe_redirect()
.