我通过在函数中添加重写规则修复了此问题。顶部的php文件:
function melon_rewrites($rules) {
$new_rules = array(
\'parties/([^/]*)/([^/]*)$\' => \'index.php?melon-parties=$matches[2]\'
);
$rules = $new_rules + $rules;
return $rules;
}
add_filter(\'rewrite_rules_array\', \'melon_rewrites\');
然后,我更改了cpt以允许查询变量:
function register_custom_post_types() {
$args = array(
\'labels\' => array(
\'name\' => __( \'Parties\' ),
\'singular_name\' => __( \'Party\' )
),
\'menu_position\' => 50,
\'public\' => true,
\'has_archive\' => \'true\',
\'hierarchical\' => true,
\'taxonomies\' => array(\'party-tags\'),
\'capability_type\' => \'post\',
\'query_var\' => true,
\'rewrite\' => false,
\'supports\' => array(\'title\', \'editor\', \'thumbnail\', \'revisions\', \'page-attributes\')
);
register_post_type(\'melon-parties\', $args);
$args = array(
\'labels\' => array(
\'name\' => __( \'Type of Parties\' ),
\'singular_name\' => __( \'Type of Party\' )
),
\'menu_position\' => 50,
\'public\' => true,
\'has_archive\' => \'true\',
\'hierarchical\' => true,
\'show_ui\' => true,
\'publicly_queryable\' => true,
\'capability_type\' => \'page\',
\'query_var\' => true,
\'supports\' => array(\'title\', \'thumbnail\', \'revisions\', \'page-attributes\'),
\'rewrite\' => false
);
register_post_type(\'party-categories\', $args);
$args = array(
\'labels\' => array(
\'name\' => __( \'Home Slider\' ),
\'singular_name\' => __( \'Home Slide\' )
),
\'menu_position\' => 50,
\'public\' => false,
\'show_ui\' => true,
\'hierarchical\' => false,
\'publicly_queryable\' => true,
\'capability_type\' => \'post\',
\'supports\' => array(\'title\', \'editor\', \'thumbnail\', \'revisions\', \'page-attributes\')
);
register_post_type(\'melon-home-slider\', $args);
$args = array(
\'labels\' => array(
\'name\' => __( \'Case Studies\' ),
\'singular_name\' => __( \'Case Study\' )
),
\'menu_position\' => 50,
\'public\' => true,
\'hierarchical\' => false,
\'publicly_queryable\' => true,
\'capability_type\' => \'post\',
\'rewrite\' => array(\'with_front\' => false, \'slug\' => \'case-studies\'),
\'supports\' => array(\'title\', \'editor\', \'thumbnail\', \'revisions\', \'page-attributes\')
);
register_post_type(\'melon-case-studies\', $args);
}
add_action( \'init\', \'register_custom_post_types\' );
我还必须对我的重写规则做一些更改,因为我实际上并没有指向我的cpt:
add_action(\'init\', \'tdd_add_rewrite_rules\');
function tdd_add_rewrite_rules() {
// Register custom rewrite rules
add_rewrite_tag(\'%melon-parties%\', \'([^/]+)\');
add_rewrite_tag(\'%party-categories%\', \'([^/]+)\');
add_permastruct(\'melon-parties\', \'parties/%party-categories%/%melon-parties%\', false);
}
add_filter(\'post_type_link\', \'tdd_permalinks\', 10, 3);
function tdd_permalinks($permalink, $post, $leavename) {
$no_data = \'no-category\';
$post_id = $post->ID;
if($post->post_type != \'melon-parties\' || empty($permalink) || in_array($post->post_status, array(\'draft\', \'pending\', \'auto-draft\')))
return $permalink;
$var1 = get_post_meta($post_id, \'cmb-event_category\', true);
$var1 = get_the_title($var1);
$var1 = sanitize_title($var1);
if(!$var1) { $var1 = $no_data; }
$permalink = str_replace(\'%party-categories%\', $var1, $permalink);
return $permalink;
}
add_action(\'init\', \'partyCategoryRewriteRules\');
function partyCategoryRewriteRules() {
// Register custom rewrite rules
global $wp_rewrite;
$wp_rewrite->add_rewrite_tag(\'%party-categories%\', \'([^/]+)\', \'party-categories=\');
$wp_rewrite->add_permastruct(\'party-categories\', \'/parties/%party-categories%\', false);
}
add_filter(\'post_type_link\', \'partyCategoryPermalinks\', 10, 3);
function partyCategoryPermalinks($permalink, $post, $leavename) {
$no_data = \'no-category\';
$post_id = $post->ID;
if($post->post_type != \'party-categories\' || empty($permalink) || in_array($post->post_status, array(\'draft\', \'pending\', \'auto-draft\')))
return $permalink;
$var1 = $post->post_name;
$var1 = sanitize_title($var1);
if(!$var1) { $var1 = $no_data; }
$permalink = str_replace(\'%party-categories%\', $var1, $permalink);
return $permalink;
}
现在我的url结构正确了