Advertisement
Guest User

SO - Question/396141

a guest
Sep 27th, 2021
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. <?php
  2.  
  3. // Custom Post Type
  4. //==========================================================================================
  5. // Community Posts =============================================================================
  6. //==========================================================================================
  7. function cws_register_community_post() {
  8. $labels = array(
  9. 'name' => _x( 'Community Posts', 'post type general name', 'cws' ),
  10. 'singular_name' => _x( 'Community Post', 'post type singular name', 'cws' ),
  11. 'add_new' => _x( 'Add New', 'community_post', 'cws' ),
  12. 'add_new_item' => __( 'Add New Community Post', 'cws' ),
  13. 'edit_item' => __( 'Edit Community Post', 'cws' ),
  14. 'new_item' => __( 'New Community Post', 'cws' ),
  15. 'all_items' => __( 'Community Posts', 'cws' ),
  16. 'view_item' => __( 'View Community Post', 'cws' ),
  17. 'search_items' => __( 'Search Community Posts', 'cws' ),
  18. 'not_found' => __( 'No Community Posts found', 'cws' ),
  19. 'not_found_in_trash' => __( 'No Community Posts found in the trash', 'cws' ),
  20. 'parent_item_colon' => '',
  21. 'menu_name' => 'Community Posts'
  22. );
  23. $args = array(
  24. 'labels' => $labels,
  25. 'description' => __( 'All Community Posts.', 'cws' ),
  26. 'public' => true,
  27. 'menu_position' => 25,
  28. // here
  29. 'show_in_menu' => true,
  30. //'show_in_menu' => 'community-menu',
  31. // 'hierarchical' => true,
  32. 'supports' => array( 'title', 'editor', 'thumbnail', 'page-attributes' ),
  33. 'has_archive' => false,
  34. // here
  35. 'taxonomies' => array( 'community-menu' ),
  36. 'rewrite' => array(
  37. 'slug' => 'community/%community_post_domain%',
  38. // here
  39. 'with_front' => true
  40. ),
  41. 'capability_type' => 'community_post',
  42. 'capabilities' => array(
  43. 'read_post' => 'read_community_posts',
  44. 'edit_post' => 'edit_community_posts',
  45. 'delete_post' => 'delete_community_posts',
  46. ),
  47. );
  48. register_post_type( 'community_post', $args );
  49. }
  50. add_action( 'init', 'cws_register_community_post' );
  51.  
  52.  
  53.  
  54. // Taxonomies
  55. //==========================================================================================
  56. // Community Post Type & Domain =============================================================
  57. //==========================================================================================
  58.  
  59. function create_community_post_taxonomies() {
  60. $domainArgs = array(
  61. 'show_ui' => true,
  62. 'hierarchical' => true,
  63. 'show_in_menu' => 'community-menu',
  64. 'show_admin_column' => true,
  65.  
  66. 'labels' => array(
  67. 'name' => _x( 'Community Post Domains', 'taxonomy general name', 'cws' ),
  68. 'singular_name' => _x( 'Community Post Domain', 'taxonomy singular name', 'cws' ),
  69. 'add_new_item' => __( 'Add New Community Post Domain', 'cws' ),
  70. 'new_item_name' => __( 'New Community Post Domain', 'cws' ),
  71. ),
  72.  
  73. // here: Remove capabilities
  74. //'capabilities' => array(
  75. // 'manage_terms' => 'edit_community_posts',
  76. // 'edit_terms' => 'edit_community_posts',
  77. // 'delete_terms' => 'edit_community_posts',
  78. // 'assign_terms' => 'edit_community_posts',
  79. //),
  80. );
  81.  
  82. $typeArgs = array(
  83. 'show_ui' => true,
  84. 'hierarchical' => true,
  85. 'show_in_menu' => 'community-menu',
  86. 'show_admin_column' => true,
  87.  
  88. 'labels' => array(
  89. 'name' => _x( 'Community Post Types', 'taxonomy general name', 'cws' ),
  90. 'singular_name' => _x( 'Community Post Type', 'taxonomy singular name', 'cws' ),
  91. 'add_new_item' => __( 'Add New Community Post Type', 'cws' ),
  92. 'new_item_name' => __( 'New Community Post Type', 'cws' ),
  93. ),
  94.  
  95. // here: Remove capabilities
  96. //'capabilities' => array(
  97. // 'manage_terms' => 'edit_community_posts',
  98. // 'edit_terms' => 'edit_community_posts',
  99. // 'delete_terms' => 'edit_community_posts',
  100. // 'assign_terms' => 'edit_community_posts',
  101. //),
  102.  
  103. 'rewrite' => array(
  104. 'slug' => 'community',
  105. // here
  106. 'with_front' => true,
  107. 'hierarchical' => false,
  108. ),
  109.  
  110. );
  111. register_taxonomy( 'community_post_type', 'community_post', $typeArgs );
  112. register_taxonomy( 'community_post_domain', 'community_post', $domainArgs );
  113. }
  114. add_action( 'init', 'create_community_post_taxonomies' );
  115.  
  116.  
  117.  
  118.  
  119. /**
  120. * here
  121. * Fix permalinks structure with post_type_link
  122. */
  123.  
  124. add_filter('post_type_link', 'so_update_permalink_structure', 10, 2);
  125. function so_update_permalink_structure( $post_link, $post )
  126. {
  127. if ( false !== strpos( $post_link, '%community_post_domain%' ) ) {
  128. $taxonomy_terms = get_the_terms( $post->ID, 'community_post_domain' );
  129. foreach ( $taxonomy_terms as $term ) {
  130. if ( ! $term->parent ) {
  131. $post_link = str_replace( '%community_post_domain%', $term->slug, $post_link );
  132. }
  133. }
  134. }
  135. return $post_link;
  136. }
  137.  
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement