关于这个话题的问题已经很少了,但它们对我的问题没有帮助。所以我想要实现的是基于ACF(radio)值的动态CPT permalink结构。
// _cb stands for checkbox
radio_acf_cb = foo
radio_acf_cb = bar
根据用户的选择,permalink将如下所示// fixed_word is the first part of the permalink which will be static
// foo/bar are the values based on the choice from the ACF
// 123 is the post ID
example.com/fixed_word/foo/123 // if foo is chosen
example.com/fixed_word/bar/123 // if bar is chosen
以下是我迄今为止所做的尝试:add_filter( \'post_type_link\', \'so51217355_post_type_link\', 10, 2 );
function so51217355_post_type_link( $permalink, $post ) {
if ( \'my_cpt\' === $post->post_type ) {
$choice = get_field(\'foo_bar\', $post->ID) // foo or bar
$choice_slug = $choice == \'foo\' ? $choce : \'bar\';
if ( $choice_slug ) {
// This would give me example.com/fixed_word/foo/123
// or example.com/fixed_word/bar/123
$permalink = \'/fixed_word/\' . $choice_slug . $post->ID;
}
}
return $permalink;
}
现在我知道我需要用add_rewrite_rule()
但我不知道如何设置条件。