Guest User

Untitled

a guest
Jan 21st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. $rewrite = [
  2. 'slug' => 'single-link',
  3. 'with_front' => false,
  4. 'pages' => false,
  5. 'feeds' => false,
  6. ];
  7. $args = [
  8. 'label' => esc_html__( 'Single Link', 'single-link' ),
  9. 'labels' => $labels,
  10. 'supports' => [ 'title' ],
  11. 'hierarchical' => false,
  12. 'public' => true,
  13. 'show_ui' => true,
  14. 'show_in_menu' => true,
  15. 'menu_position' => 15,
  16. 'menu_icon' => 'dashicons-admin-links',
  17. 'show_in_admin_bar' => false,
  18. 'show_in_nav_menus' => true,
  19. 'can_export' => true,
  20. 'has_archive' => false,
  21. 'exclude_from_search' => true,
  22. 'publicly_queryable' => true,
  23. 'rewrite' => $rewrite,
  24. 'capability_type' => 'page',
  25. 'show_in_rest' => true,
  26. ];
  27.  
  28. $args = apply_filters( 'single-link/post_type/args', $args );
  29. register_post_type( 'single-link', $args );
  30.  
  31. function remove_cpt_slug( $post_link, $post, $leavename ) {
  32.  
  33. if ( 'single-link' != $post->post_type || 'publish' != $post->post_status ) {
  34. return $post_link;
  35. }
  36.  
  37. $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
  38.  
  39. return $post_link;
  40. }
  41. add_filter( 'post_type_link', 'remove_cpt_slug', 10, 3 );
  42.  
  43.  
  44. function change_slug_struct( $query ) {
  45.  
  46. if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
  47. return;
  48. }
  49.  
  50. if ( ! empty( $query->query['name'] ) ) {
  51. $query->set( 'post_type', array( 'post', 'single-link', 'page' ) );
  52. }
  53. }
  54. add_action( 'pre_get_posts', 'change_slug_struct' );
Add Comment
Please, Sign In to add comment