Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. add_action( 'init', 'models_post_type' );
  2. function models_post_type() {
  3. register_post_type( 'models',
  4. array(
  5. 'labels' => array(
  6. 'name' => __( 'Models' ),
  7. 'singular_name' => __( 'Models' ),
  8. ),
  9. 'public' => true,
  10. 'has_archive' => 'model_category',
  11. 'menu_icon' => 'dashicons-id',
  12. 'rewrite' => array('slug' => 'categories/%model_category%','with_front' => false),
  13. 'supports' => array('title','editor','thumbnail','comments'),
  14. /* "cptp_permalink_structure" => "/categories/%model_category%/%postname%"*/
  15. )
  16. );
  17. }
  18.  
  19.  
  20.  
  21. add_action( 'init', 'model_taxonomies', 0 );
  22. function model_taxonomies(){
  23. register_taxonomy(
  24. 'model_category',
  25. 'models',
  26. array(
  27. 'hierarchical' => true,
  28. 'label' => __('Model category'),
  29. 'show_ui' => true,
  30. 'show_admin_column' => true,
  31. 'query_var' => true,
  32. 'rewrite' => array(
  33. 'slug' => 'categories',
  34. 'with_front' => true,
  35. 'hierarchical' => false
  36. ),
  37. )
  38. );
  39. }
  40.  
  41.  
  42. add_filter( 'post_type_link', 'wpa_show_permalinks', 1, 2 );
  43.  
  44. function wpa_show_permalinks( $post_link, $post ){
  45. $list_posts = array(
  46. 'categories' => 'model_category',
  47. );
  48.  
  49. foreach ($list_posts as $cpt => $tax){
  50. if ( is_object( $post ) && $post->post_type == $cpt ){
  51. $terms = wp_get_object_terms( $post->ID, $tax );
  52. $term_slug = $terms ? $terms[0]->slug : 'no-cat';
  53.  
  54. return str_replace( '%'.$tax.'%' ,$term_slug, $post_link );
  55. }
  56. }
  57. return $post_link;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement