如何自定义 custom post type 的permalink

在WordPress添加custom post type之后,默认的permanlink组织形式是

http://example.com/post-type/post-name

如果想改为

http://example.com/post-type/%post_id%

应该如何做?根据网上搜到的教程,下面这段代码(以post_type=”business”为例)帮我实现了想要的功能。把它放入主题文件夹的functions.php或者专门新建一个插件都可以。插件激活之后要去访问一下后台permalink的设置页,不然新的permalink链接会有“页面无法显示”的错误。

[php]
add_filter(‘post_type_link’, ‘ct_post_type_link’, 1, 3);

function ct_post_type_link( $post_link, $post = 0 ){
$post_type = $post->post_type;

if ( $post_type == ‘business’ ) {
return home_url( "/$post_type/" . $post->ID);
}else {
return $post_link;
}
}

add_action( ‘init’, ‘ct_rewrites_init’ );
function ct_rewrites_init(){
add_rewrite_rule(
‘business/([0-9]+)?$’,
‘index.php?post_type=business&p=$matches[1]’,
‘top’ );
}
[/php]

如何在WordPress的默认Query添加Where条件

[php]
function filter_where($where = ”) {
$where .= " AND post_date >= ‘2009-03-17’ AND post_date <= ‘2009-05-03’";
return $where;
}
add_filter(‘posts_where’, ‘filter_where’);
query_posts($query_string);
while (have_posts()) :
the_post();
the_content();
endwhile;

[/php]