我们将从分类法开始,因为它们相当简单。如果您注册这些reading/genre
和reading/author
作为slug
争论,这些应该没有问题。
帖子类型有点复杂。为此,我们向rewrite
参数设置为justtrue
. 然后,我们为该post类型添加一个新的permastruct,其中包含:
add_permastruct(
\'pg_review\',
"/reading/%year%/%monthnum%/%day%/%pg_review%/",
array( \'with_front\' => false )
);
现在我们有了单帖子视图的规则,但年/月/日视图都设置了错误的帖子类型,因此我们将添加一些新规则来重置这些规则:
add_rewrite_rule(
\'^reading/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$\',
\'index.php?post_type=pg_review&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]\',
\'top\'
);
add_rewrite_rule(
\'^reading/([0-9]{4})/([0-9]{1,2})/?$\',
\'index.php?post_type=pg_review&year=$matches[1]&monthnum=$matches[2]\',
\'top\'
);
add_rewrite_rule(
\'^reading/([0-9]{4})/?$\',
\'index.php?post_type=pg_review&year=$matches[1]\',
\'top\'
);
最后一步是过滤
post_type_link
要在永久链接中插入正确的年/月/日,请执行以下操作:
function wpd_pg_review_permalinks( $url, $post ) {
if ( \'pg_review\' == get_post_type( $post ) ) {
$url = str_replace( "%year%", get_the_date(\'Y\'), $url );
$url = str_replace( "%monthnum%", get_the_date(\'m\'), $url );
$url = str_replace( "%day%", get_the_date(\'d\'), $url );
}
return $url;
}
add_filter( \'post_type_link\', \'wpd_pg_review_permalinks\', 10, 2 );