Advertisement
Guest User

CPT permalink with taxonomy values

a guest
Jan 10th, 2013
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.52 KB | None | 0 0
  1. add_action('init', 'custom_init');
  2. add_filter('post_type_link', 'english_permalink', 1, 3);
  3.  
  4. function custom_init(){
  5.         register_post_type('english-posts',
  6.             array('label' => 'English',
  7.                 'query_var' => true,
  8.                 'rewrite' => false
  9.             )
  10.         );
  11.        
  12.         register_taxonomy('english-categories',
  13.             'english-posts',
  14.             array('query_var' => true,
  15.                 'rewrite' => true
  16.             )
  17.         );
  18.  
  19.     global $wp_rewrite;
  20.     $english_structure = '/%english-categories%/%english-posts%';
  21.     $wp_rewrite->add_rewrite_tag("%english-posts%", '([^/]+)', "english-posts=");
  22.     $wp_rewrite->add_permastruct('english-posts', $english_structure, false);
  23. }
  24.  
  25. function english_permalink($permalink, $post_id, $leavename){
  26.     $post = get_post($post_id);
  27.  
  28.     $rewritecode = array(
  29.     '%english-categories%',
  30.     $leavename? '' : '%postname%',
  31.     $leavename? '' : '%pagename%',
  32.     );
  33.  
  34.     if('' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft'))){
  35.  
  36.         if (strpos($permalink, '%english-categories%') !== FALSE){
  37.         $terms = wp_get_object_terms($post->ID, 'english-categories');  
  38.         if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $english_categories = $terms[0]->slug;
  39.         else $english_categories = 'unassigned';
  40.         }
  41.  
  42.     $rewritereplace = array(
  43.         $english_categories,
  44.         $post->post_name,
  45.         $post->post_name,
  46.     );
  47.     $permalink = str_replace($rewritecode, $rewritereplace, $permalink);
  48.     }
  49.     else{
  50.     }
  51.     return $permalink;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement