我正在使用 post_type_link 挂钩将自定义分类添加到永久链接中。 它工作得很好,除了现在新的永久链接只是重定向到登陆页面 - 它不使用我的单个页面。如果您更改自定义帖子类型的永久链接,如何保持单页面正常工作。此外,常规页面现在也链接到登陆页面。 我已经在 WP 管理中重新保存了永久链接,但没有成功。 有什么想法吗?
代码:
function update_permalink_agendaitem( $post_link, $post, $leavename = true, $sample = false ){
if (get_post_type($post->ID) == 'agendaitem') {
if ( is_object( $post ) ){
$terms = wp_get_object_terms( $post->ID, 'agendaitemtype' );
if( $terms ){
return str_replace( '%agendaitemtype%' , get_post_type($post->ID) . '/' . $terms[0]->slug . $post->slug , $post_link );
}
}
} else {
return $post_link;
}
}
add_filter( 'post_type_link', 'update_permalink_agendaitem', 1, 3 );
然后在构建帖子类型时:
'rewrite' => array('slug' => '%agendaitemtype%', 'with_front' => false)
更新:所以我认为我需要做的是根据 CPT 类型重新路由到适当的单页面。 我找到了下面的代码来执行此操作,但看起来这个钩子 single_template 永远不会被调用???
function update_single_templatee( $single_template ) {
wp_die("WHEN DOES THIS FIRE???");
}
add_filter( 'single_template', 'update_single_template', 11 );
...什么也没发生。我尝试过不同的优先级,但仍然一无所获......
所以...看起来像。 如果我尝试使用挂钩 post_type_link 来更新 URL 结构(来自上面的第一段代码),那么 single_template 挂钩(我的第二段代码)将不再为 CPT 触发。 如果我取出 post_type_link 钩子回调,那么 single_template 钩子就会触发。