我试着将死亡医生的建议应用于this post 要实现以下URL结构:
实例com/activities=WP页
示例。com/activities/type term=自定义存档示例。com/活动/类型术语/活动帖子=自定义帖子
所有URL的工作,直到返回Wordpress“not found”错误的最低URL为止。
我不知道是否需要过滤重写规则才能使其正常工作。我在上面链接的帖子表明,我已经完成了所需的一切。
任何帮助都将不胜感激,谢谢。这是我的代码:
/*
* Register custom content objects on initiation
*/
add_action(\'init\', \'register_type\'); // Type taxonomy (activities)
add_action(\'init\', \'register_activity\'); // Activity post-type
add_filter(\'post_type_link\', \'filter_activity_link\', 10, 2); // Filter permalinks for activity post-type
/*
* Register Type custom taxonomy for activities
*/
function register_type() {
$labels = array(
\'name\' => (\'Types\'),
\'singular_name\' => (\'Type\'),
\'add_new\' => (\'Add New Type\'),
\'add_new_item\' => (\'Add New Type\'),
\'edit_item\' => (\'Edit Type\'),
\'new_item\' => (\'New Type\'),
\'view_item\' => (\'View Type\'),
\'search_items\' => (\'Search Type\'),
);
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'activities\', \'with_front\' => false ),
);
register_taxonomy( \'type\', \'activity\', $args );
}
/*
* Register Activity custom post-type
*/
function register_activity() {
$labels = array(
\'name\' => (\'Activities\'),
\'singular_name\' => (\'Activity\'),
\'all_items\' => (\'All Activities\'),
\'add_new_item\' => (\'Add New Activity\'),
\'edit_item\' => (\'Edit Activity\'),
\'new_item\' => (\'New Activity\'),
\'view_item\' => (\'View Activity\'),
\'search_items\' => (\'Search Activities\')
);
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'show_in_nav_menus\' => false, // Do not appear directly in navigation
\'menu_position\' => 5,
\'hierarchical\' => false,
\'query_var\' => true,
\'supports\' => array( \'title\', \'editor\', \'type\', \'revisions\', \'comments\' ),
\'rewrite\' => array( \'slug\' => \'activities/%tax_type%\', \'with_front\' => false )
);
register_post_type( \'activity\', $args );
}
/*
* Update Type rewrite slug with applicable terms
*/
function filter_activity_link($link, $post) {
if ($post->post_type != \'activity\')
return $link;
if ($cats = get_the_terms($post->ID, \'type\'))
$link = str_replace(\'%tax_type%\', array_pop($cats)->slug, $link);
return $link;
}
更新好的,您总是需要添加重写规则才能完成此过程。URL可能显示正确,但数据库无法理解。
This post 极大地帮助了我
this reference 有关regex的信息。
我只需要添加代码
add_rewrite_rule(\'^activities/([^/]*)/([^/]*)/?$\' ,\'index.php?activity=$matches[2]\',\'top\';
希望这对别人有帮助。