Created
July 24, 2015 10:35
-
-
Save atomtigerzoo/d21bb4d487c41d7f68ea to your computer and use it in GitHub Desktop.
Wordpress: Custom post type with post_id-only in URLs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Replace 'mycustomname' with the actual custom type post | |
* name to fit your needs. | |
* | |
* !! Flush rewrite rules in Wordpress backend afterwards !! | |
*/ | |
/** | |
* Create the CTP | |
*/ | |
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'); | |
/** | |
* Correct post links for CTP | |
*/ | |
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); | |
/** | |
* Rewrite rules for the CTP | |
*/ | |
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'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment