admin管理员组文章数量:1129455
I have created a custom post type in Wordpress which is working great. The only thing I need to solve are the permalinks created by this CTP.
I currently have:
example/<custom-post-name>/<post-id>/<postname>/
I want it to only display the post-id:
example/<custom-post-name>/<post-id>/
The custom post type is registered with:
function create_post_type_mycustomname() {
$args = array(
'capability_type' => 'post',
'has_archive' => 'mycustomname',
'rewrite' => array(
'slug' => '/mycustomname/%post_id%',
'feeds' => false
)
);
register_post_type('ctp_mycustomname', $args);
}
add_action('init', 'create_post_type_mycustomname');
And the %post_id
in the slug is getting replaced with the following:
function custom_post_mycustomname_link($post_link, $post = 0, $leavename = false) {
if($post->post_type == 'ctp_mycustomname') {
return str_replace('%post_id%', $post->ID, $post_link);
}
else {
return $post_link;
}
}
add_filter('post_type_link', 'custom_post_mycustomname_link', 1, 3);
Any hints on how to strip the postname from these kinds of URLs?
I have created a custom post type in Wordpress which is working great. The only thing I need to solve are the permalinks created by this CTP.
I currently have:
example.com/<custom-post-name>/<post-id>/<postname>/
I want it to only display the post-id:
example.com/<custom-post-name>/<post-id>/
The custom post type is registered with:
function create_post_type_mycustomname() {
$args = array(
'capability_type' => 'post',
'has_archive' => 'mycustomname',
'rewrite' => array(
'slug' => '/mycustomname/%post_id%',
'feeds' => false
)
);
register_post_type('ctp_mycustomname', $args);
}
add_action('init', 'create_post_type_mycustomname');
And the %post_id
in the slug is getting replaced with the following:
function custom_post_mycustomname_link($post_link, $post = 0, $leavename = false) {
if($post->post_type == 'ctp_mycustomname') {
return str_replace('%post_id%', $post->ID, $post_link);
}
else {
return $post_link;
}
}
add_filter('post_type_link', 'custom_post_mycustomname_link', 1, 3);
Any hints on how to strip the postname from these kinds of URLs?
Share Improve this question asked Jul 24, 2015 at 9:02 lorem monkeylorem monkey 3471 gold badge3 silver badges12 bronze badges2 Answers
Reset to default 13I found the answer myself - so here is the update to above problem:
Custom post type registration:
function create_post_type_mycustomname() {
$args = array(
'capability_type' => 'post',
'has_archive' => 'mycustomname',
'rewrite' => array(
'slug' => '/mycustomname',
'feeds' => false
)
);
register_post_type('ctp_mycustomname', $args);
}
add_action('init', 'create_post_type_mycustomname');
Change the links:
function mycustomname_links($post_link, $post = 0) {
if($post->post_type === 'ctp_mycustomname') {
return home_url('mycustomname/' . $post->ID . '/');
}
else{
return $post_link;
}
}
add_filter('post_type_link', 'mycustomname_links', 1, 3);
Add the correct rewrites rules:
function mycustomname_rewrites_init(){
add_rewrite_rule('mycustomname/([0-9]+)?$', 'index.php?post_type=ctp_mycustomname&p=$matches[1]', 'top');
}
add_action('init', 'mycustomname_rewrites_init');
Flush rewrite-rules in Wordpress backend afterwards and you are good to go!
This solution fails when you want to offer the oembed capability eg, example.com///embed
Using custom-post-type-permalinks did retain this capability.
本文标签: Custom post type permalink only use postid and remove postname
版权声明:本文标题:Custom post type permalink: only use %post_id% and remove %postname% 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736712453a1949019.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论