Wordpress Custom Post Type : POST ID in post url


/ Published in: PHP
Save to your folder(s)

so you want to have your url structure like :
yourdomain.com/post-type/286

add the following snippet to your functions.php and change the post-type names where appropriate


Copy this code and paste it in your HTML
  1. add_action('init', 'post-type_rewrite');
  2. function post-type_rewrite() {
  3. global $wp_rewrite;
  4. $queryarg = 'post_type=post-type&p=';
  5. $wp_rewrite->add_rewrite_tag('%post_id%', '([^/]+)', $queryarg);
  6. $wp_rewrite->add_permastruct('post-type', '/post-type/%post_id%', false);
  7. }
  8.  
  9. add_filter('post_type_link', 'post-type_permalink', 1, 3);
  10. function post-type_permalink($post_link, $id = 0, $leavename, $sample) {
  11. global $wp_rewrite;
  12. $post = &get_post($id);
  13. if ( is_wp_error( $post ) )
  14. return $post;
  15. $newlink = $wp_rewrite->get_extra_permastruct('bodumanzaru');
  16. $newlink = str_replace("%post_id%", $post->ID, $newlink);
  17. $newlink = home_url(user_trailingslashit($newlink));
  18. return $newlink;
  19. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.