在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]

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s