Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. domain.com/custom-post-type-slug/single-slug/sub-single-slug/
  2.  
  3. function wpse_register_custom_post_type() {
  4.  
  5. $labels = array(
  6. "name" => __( 'custom post type', 'text-domain' ),
  7. "singular_name" => __( 'custom post types', 'text-domain' ),
  8. );
  9.  
  10. $args = array(
  11. "label" => __( 'custom post type', 'text-domain' ),
  12. "labels" => $labels,
  13. "description" => "",
  14. "public" => true,
  15. "publicly_queryable" => true,
  16. "show_ui" => true,
  17. "show_in_menu" => true,
  18. "capability_type" => "post",
  19. "map_meta_cap" => true,
  20. "hierarchical" => true,
  21. "rewrite" => array( "slug" => "custom_post_type", "with_front" => true ),
  22. "query_var" => true,
  23. "supports" => array( "title", "editor", "thumbnail", "custom-fields", "page-attributes" ),
  24. "taxonomies" => array( "category", "post_tag" ),
  25. );
  26.  
  27. register_post_type( "custom_post_type", $args );
  28. }
  29.  
  30. add_action( 'init', 'wpse_register_custom_post_type' );
  31.  
  32. // Fake pages' permalinks and titles. Change these to your required sub pages.
  33. $my_fake_pages = array(
  34. 'reviews' => 'Reviews',
  35. 'purchase' => 'Purchase',
  36. 'author' => 'Author Bio'
  37. );
  38.  
  39. add_filter('rewrite_rules_array', 'fsp_insertrules');
  40. add_filter('query_vars', 'fsp_insertqv');
  41.  
  42. // Adding fake pages' rewrite rules
  43. function wpse_261271_insertrules($rules)
  44. {
  45. global $my_fake_pages;
  46.  
  47. $newrules = array();
  48. foreach ($my_fake_pages as $slug => $title)
  49. $newrules['books/([^/]+)/' . $slug . '/?$'] = 'index.php?books=$matches[1]&fpage=' . $slug;
  50.  
  51. return $newrules + $rules;
  52. }
  53.  
  54. // Tell WordPress to accept our custom query variable
  55. function wpse_261271_insertqv($vars)
  56. {
  57. array_push($vars, 'fpage');
  58. return $vars;
  59. }
  60.  
  61. // Remove WordPress's default canonical handling function
  62.  
  63. remove_filter('wp_head', 'rel_canonical');
  64. add_filter('wp_head', 'fsp_rel_canonical');
  65. function wpse_261271_rel_canonical()
  66. {
  67. global $current_fp, $wp_the_query;
  68.  
  69. if (!is_singular())
  70. return;
  71.  
  72. if (!$id = $wp_the_query->get_queried_object_id())
  73. return;
  74.  
  75. $link = trailingslashit(get_permalink($id));
  76.  
  77. // Make sure fake pages' permalinks are canonical
  78. if (!empty($current_fp))
  79. $link .= user_trailingslashit($current_fp);
  80.  
  81. echo '<link rel="canonical" href="'.$link.'" />';
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement