下面是一个完整的工作示例,它添加了一个post类型,并使用额外的规则来捕获额外的参数:
function wpd_post_type_and_rule() {
register_post_type( \'mycpt\',
array(
\'labels\' => array(
\'name\' => __( \'mycpt\' ),
),
\'public\' => true,
\'rewrite\' => array( \'slug\' => \'mycpt\' ),
)
);
add_rewrite_tag( \'%mycpt_var%\', \'([^/]*)\' );
add_rewrite_rule(
\'^mycpt/([^/]*)/([^/]*)/?$\',
\'index.php?mycpt=$matches[1]&mycpt_var=$matches[2]\',
\'top\'
);
}
add_action( \'init\', \'wpd_post_type_and_rule\' );
添加此规则和刷新重写规则后,您将同时拥有
www.site.com/mycpt/the-name-of-my-post/
以及
www.site.com/mycpt/the-name-of-my-post/var-value-here/
您可以获得
mycpt_var
在模板中使用:
echo get_query_var( \'mycpt_var\' );