我已经找到了a working solution 我的问题是,但是the problem is that it neutralizes another function 这会将类别库添加到自定义的post类型permalinks中,我需要这两种类型,所以I must to merge them somehow. 如何做到这一点?
这是添加的代码。自定义帖子类型的html扩展:
//Create the rewrite rules like post-type/post-name.html
add_action( \'rewrite_rules_array\', \'rewrite_rules\' );
function rewrite_rules( $rules ) {
$new_rules = array();
foreach ( get_post_types() as $t )
$new_rules[ $t . \'/([^/]+)\\.html$\' ] = \'index.php?post_type=\' . $t . \'&name=$matches[1]\';
return $new_rules + $rules;
}
//Format the new permalink structure for these post types.
add_filter( \'post_type_link\', \'custom_post_permalink\' ); // for cpt post_type_link (rather than post_link)
function custom_post_permalink ( $post_link ) {
global $post;
$type = get_post_type( $post->ID );
return home_url( $type . \'/\' . $post->post_name . \'.html\' );
}
//And then stop redirecting the canonical URLs to remove the trailing slash.
add_filter( \'redirect_canonical\', \'__return_false\' );
这是将类别库添加到自定义帖子类型的代码(请参见a similar solution, 和another)://Change your rewrite to add the course query var:
\'rewrite\' => array(\'slug\' => \'courses/%course%\')
//Then filter post_type_link to insert the selected course into the permalink:
function wpa_course_post_link( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) ){
$terms = wp_get_object_terms( $post->ID, \'course\' );
if( $terms ){
return str_replace( \'%course%\' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( \'post_type_link\', \'wpa_course_post_link\', 1, 3 );