我遇到了另一个精彩的404问题。我正在尝试使用两种不同的帖子类型,它们共享相同的重写段。我刷新了我的重写规则,当我测试其中一个CPT时,另一个得到404。我希望对两者都使用的重写是:
\'rewrite\' => array(\'slug\' => \'team/%teamtype%\'),
有人知道怎么处理吗?
add_action( \'init\', \'create_rider_type\' );
function create_rider_type() {
register_post_type(\'riders\',
array(
\'description\' => \'rider custom post type\',
\'show_ui\' => true,
\'menu_position\' => 5,
\'menu_icon\' => get_stylesheet_directory_uri() . \'/images/riders.png\',
\'exclude_from_search\' => false,
\'labels\' => array(
\'name\' => \'Team Riders\',
\'singular_name\' => \'Rider\',
\'add_new\' => \'Add New rider\',
\'add_new_item\' => \'Add New rider\',
\'edit\' => \'Edit riders\',
\'edit_item\' => \'Edit rider\',
\'new_item\' => \'New rider\',
\'view\' => \'View rider\',
\'view_item\' => \'View rider\',
\'search_items\' => \'Search riders\',
\'not_found\' => \'No riders found\',
\'not_found_in_trash\' => \'No riders found in Trash\',
\'parent\' => \'Parent rider\',
),
\'hierarchical\' => false,
\'supports\' => array(\'title\',\'editor\',\'excerpt\', \'trackbacks\',\'custom-fields\', \'comments\',\'revisions\',\'thumbnail\',\'author\',\'page-attributes\'),
\'public\' => true,
\'rewrite\' => array(\'slug\' => \'team/%teamtype%\'),
\'taxonomies\' => array(\'teamtype\')
)
);
}
add_action( \'init\', \'create_sponsor_type\' );
function create_sponsor_type() {
register_post_type(\'sponsors\',
array(
\'description\' => \'sponsor custom post type\',
\'show_ui\' => true,
\'menu_position\' => 5,
\'menu_icon\' => get_stylesheet_directory_uri() . \'/images/sponsors.png\',
\'exclude_from_search\' => false,
\'labels\' => array(
\'name\' => \'Team sponsors\',
\'singular_name\' => \'sponsor\',
\'add_new\' => \'Add New sponsor\',
\'add_new_item\' => \'Add New sponsor\',
\'edit\' => \'Edit sponsors\',
\'edit_item\' => \'Edit sponsor\',
\'new_item\' => \'New sponsor\',
\'view\' => \'View sponsor\',
\'view_item\' => \'View sponsor\',
\'search_items\' => \'Search sponsors\',
\'not_found\' => \'No sponsors found\',
\'not_found_in_trash\' => \'No sponsors found in Trash\',
\'parent\' => \'Parent sponsor\',
),
\'hierarchical\' => false,
\'supports\' => array(\'title\',\'editor\',\'excerpt\', \'trackbacks\',\'custom-fields\', \'comments\',\'revisions\',\'thumbnail\',\'author\',\'page-attributes\'),
\'public\' => true,
\'rewrite\' => array(\'slug\' => \'team/%teamtype%\'),
\'taxonomies\' => array(\'teamtype\')
)
);
}
**
***
***
*****
更新***********************************我发布的原始CPT重写代码经过了简化,因此我可以更直截了当地指出这一点,但是如果您能看到我如何使用自定义分类法处理这些永久链接,那么这可能更有意义。我已更新代码以显示。
我真的希望把它们作为单独的职位类型——对于组织,以及每个职位的单独元数据库。请检查CPT的更新重写,以及下面我的分类设置:
add_action( \'init\', \'create_team_taxonomies\' );
function create_team_taxonomies() {
register_taxonomy(
\'teamtype\',
array(\'riders\',\'sponsors\'),
array(
\'labels\' => array(
\'name\' => \'Team Types\',
\'singular_name\' => \'Team Type\',
\'search_items\' => \'Search Team Types\',
\'popular_items\' => \'Popular Team Types\',
\'all_items\' => \'All Team Types\',
\'parent_item\' => \'Parent Team Type\',
\'parent_item_colon\' => \'Parent Team Type:\',
\'edit_item\' => \'Edit Team Type\',
\'update_item\' => \'Update Team Type\',
\'add_new_item\' => \'Add New Team Type\',
\'new_item_name\' => \'New Team Type Name\'
),
\'hierarchical\' => true,
\'public\' => true,
\'show_ui\' => true,
\'query_var\' => \'teamtype\',
\'show_tagcloud\' => true,
\'rewrite\' => array( \'slug\' => \'team\', \'with_front\' => false)
)
);
}
下面是我在选择分类法并发布帖子时如何设置重写:
add_filter(\'post_link\', \'teamtypepermalink\', 10, 3);
add_filter(\'post_type_link\', \'teamtypepermalink\', 10, 3);
function teamtypepermalink($permalink, $post_id, $leavename) {
if (strpos($permalink, \'%teamtype%\') === FALSE) return $permalink;
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;
// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, \'teamtype\');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
else $taxonomy_slug = \'not-specified\';
return str_replace(\'%teamtype%\', $taxonomy_slug, $permalink);
}