我有一个自定义分类法叫做campaign
和一个名为asset
. 对于资产,我希望具有以下permalink结构:mysite.com/<campaign_name>/<asset_name>
. 我已经通过以下代码实现了这一点,但是现在如果我转到任何具有url结构的正常页面mysite.com/<pagename>
它给出了404。当我注释掉函数中用于注册自定义post类型的重写slug部分,或者添加此部分时ams/%campaign%
, 它可以工作,但这不是我想要的自定义帖子类型的URL结构。
用于注册自定义分类的代码:
...
\'rewrite\' => array(
\'slug\' => \'\',
\'with_front\' => true,
),
...
注册自定义帖子类型的代码:...
rewrite\' => array(
\'slug\' => \'%campaign%\',
\'with_front\' => true,
),
...
重写规则的函数:function ams_asset_add_rewrite_rules( $rules ) {
global $post;
if ($post->post_type == \'asset\' ) {
$new = array();
$new[\'([^/]+)/(.+)/?$\'] = \'index.php?asset=$matches[2]\';
$new[\'(.+)/?$\'] = \'index.php?campaign=$matches[1]\';
return array_merge( $new, $rules );
}
return $rules;
}
add_filter( \'rewrite_rules_array\', \'ams_asset_add_rewrite_rules\' );
// Handle the \'%campaign%\' URL placeholder
function ams_asset_filter_post_type_link( $link, $post = 0 ) {
if ( $post->post_type == \'asset\' ) {
$cats = wp_get_object_terms( $post->ID, \'campaign\' );
if ( $cats ) {
$link = str_replace( \'%campaign%\', $cats[0]->slug, $link );
}
}
return $link;
}
add_filter( \'post_type_link\', \'ams_asset_filter_post_type_link\', 10, 2 );
编辑:它不是重复的。我解释了为什么这个可能重复的问题不能解决我的问题。