这里有一个问题需要解决的,一个网站原本是用的别名URL,每次发布文章是需要自定义别名的。如果我们不自定义别名会直接中文URL,这个肯定不行。这不,每次更换修改别名效率太低,而且不利于采集发布的文章。这里,我们如果修改ID URL那势必也会影响以前发布的文章URL。
有没有办法可以实现自动将特定的分类文章,重新发布的时候自动以ID URL呢?
$mytypes = array(
'type1' => 'slug1',
'type2' => 'slug2',
'type3' => 'slug3'
);
add_filter('post_type_link', 'custom_book_link', 1, 3);
function custom_book_link( $link, $post = 0 ){
global $mytypes;
if ( in_array( $post->post_type,array_keys($mytypes) ) ){
return home_url( $mytypes[$post->post_type].'/' . $post->ID .'.html' );
} else {
return $link;
}
}
add_action( 'init', 'custom_book_rewrites_init' );
function custom_book_rewrites_init(){
global $mytypes;
foreach( $mytypes as $k => $v ) {
add_rewrite_rule(
$v.'/([0-9]+)?.html$',
'index.php?post_type='.$k.'&p=$matches[1]',
'top' );
}
}
这里,我们可以根据需要设置指定的分类文章,让实现自动ID URL。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END