我定义了一个自定义的帖子类型“Review”,我想将其与一个自定义的永久链接:/reviews/year/postname一起使用。下面的代码正确地创建了CPT,并且在创建新的“审阅”时,自定义永久链接正确地显示在管理面板中。但是,当单击自定义永久链接时,创建的结果“Review”返回404。现代wordpress解决这个问题的方式是什么?理想情况下,我不想使用任何外部插件。下面的代码是在自定义插件中定义的。
// Register \'Review\' custom post type
add_action( \'init\', \'vidcreate_post_types\' );
// Register the permalink structure desired for \'Review\' posts, with placeholders
add_action( \'init\', \'vidrewrite_rules\' );
// Replace permalink placeholders with actual content
add_filter(\'post_type_link\', \'update_review_link_placeholders\', 10, 2);
/**
* Custom \'Review\' post type
*/
function vidcreate_post_types() {
register_post_type( \'review\',
array(
\'labels\' => array(
\'name\' => __( \'Reviews\' ),
\'singular_name\' => __( \'Review\' )
),
\'public\' => true,
\'has_archive\' => true,
\'taxonomies\' => array(\'category\', \'post_tag\'),
\'rewrite\' => array(
\'slug\'=>\'reviews\',
\'with_front\'=> false,
\'feed\'=> true,
\'pages\'=> true
),
\'hierarchical\' => true,
\'menu_icon\' => \'dashicons-video-alt\',
\'supports\' => array(
\'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\', \'revisions\', \'page-attributes\'
)
)
);
}
/**
* Create custom permalink structure for \'Review\' pages
*/
function vidrewrite_rules(){
add_rewrite_tag( \'%review_slug%\', \'(reviews)\',\'post_type=review&slug=\' );
add_permastruct( \'review\', \'reviews/%year%/%review%\');
}
/**
* Update permalinks placeholders with content
*/
function update_review_link_placeholders($permalink, $post) {
if((\'review\' == $post->post_type) && \'\' != $permalink && !in_array($post->post_status, array(\'draft\', \'pending\', \'auto-draft\')) ) {
// currently just replacing %year % with 2014 for testing
$permalink = str_replace(\'%year%\', \'2014\', $permalink);
return $permalink;
}
}