Posted By

ssmilwaukee on 09/19/11


Tagged

post wordpress types custom permalinks redirects


Versions (?)

Wordpress permalinks for custom post types (not working)


 / Published in: PHP
 

For some unknown reason this causes my single custom post type pages to display the home page.

  1. add_action('init', 'my_videos_rewrite');
  2.  
  3. function my_videos_rewrite() {
  4.  
  5. add_rewrite_tag( '%videos%', '([^/]+)' );
  6. $extra_post_types = get_post_types( array( '_builtin' => false, 'publicly_queryable' => true ) );
  7. if( empty( $extra_post_types ) )
  8. return;
  9. add_rewrite_tag( '%post_type%', '('.implode('|',$extra_post_types).')' );
  10. add_permastruct( 'videos', '/%post_type%/%year%/%monthnum%/%videos%/', true, 1 );
  11.  
  12. }
  13.  
  14. function my_custom_permalinks_adapt( $link, $post, $leavename, $sample ){
  15. if( 'videos' != $post->post_type )
  16. return $link;
  17. $rewritecode = array(
  18. '%year%',
  19. '%monthnum%',
  20. '%day%',
  21. '%hour%',
  22. '%minute%',
  23. '%second%',
  24. $leavename? '' : '%postname%',
  25. '%post_id%',
  26. '%post_type%',
  27. $leavename? '' : '%pagename%',
  28. $leavename? '' : '%videos%',
  29. );
  30. $unixtime = strtotime($post->post_date);
  31. $date = explode(' ', date('Y m d H i s', $unixtime));
  32. $replace_array = array(
  33. $date[0],
  34. $date[1],
  35. $date[2],
  36. $date[3],
  37. $date[4],
  38. $date[5],
  39. $post->post_name,
  40. $post->ID,
  41. $post->post_type,
  42. $post->post_name,
  43. $post->post_name,
  44. );
  45. $path = str_replace($rewritecode, $replace_array, $link);
  46. return $path;
  47. }
  48.  
  49. add_action( 'post_type_link', 'my_custom_permalinks_adapt', 10, 4 );

Report this snippet  

You need to login to post a comment.