Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- domain.com/custom-post-type-slug/single-slug/sub-single-slug/
- function wpse_register_custom_post_type() {
- $labels = array(
- "name" => __( 'custom post type', 'text-domain' ),
- "singular_name" => __( 'custom post types', 'text-domain' ),
- );
- $args = array(
- "label" => __( 'custom post type', 'text-domain' ),
- "labels" => $labels,
- "description" => "",
- "public" => true,
- "publicly_queryable" => true,
- "show_ui" => true,
- "show_in_menu" => true,
- "capability_type" => "post",
- "map_meta_cap" => true,
- "hierarchical" => true,
- "rewrite" => array( "slug" => "custom_post_type", "with_front" => true ),
- "query_var" => true,
- "supports" => array( "title", "editor", "thumbnail", "custom-fields", "page-attributes" ),
- "taxonomies" => array( "category", "post_tag" ),
- );
- register_post_type( "custom_post_type", $args );
- }
- add_action( 'init', 'wpse_register_custom_post_type' );
- // Fake pages' permalinks and titles. Change these to your required sub pages.
- $my_fake_pages = array(
- 'reviews' => 'Reviews',
- 'purchase' => 'Purchase',
- 'author' => 'Author Bio'
- );
- add_filter('rewrite_rules_array', 'fsp_insertrules');
- add_filter('query_vars', 'fsp_insertqv');
- // Adding fake pages' rewrite rules
- function wpse_261271_insertrules($rules)
- {
- global $my_fake_pages;
- $newrules = array();
- foreach ($my_fake_pages as $slug => $title)
- $newrules['books/([^/]+)/' . $slug . '/?$'] = 'index.php?books=$matches[1]&fpage=' . $slug;
- return $newrules + $rules;
- }
- // Tell WordPress to accept our custom query variable
- function wpse_261271_insertqv($vars)
- {
- array_push($vars, 'fpage');
- return $vars;
- }
- // Remove WordPress's default canonical handling function
- remove_filter('wp_head', 'rel_canonical');
- add_filter('wp_head', 'fsp_rel_canonical');
- function wpse_261271_rel_canonical()
- {
- global $current_fp, $wp_the_query;
- if (!is_singular())
- return;
- if (!$id = $wp_the_query->get_queried_object_id())
- return;
- $link = trailingslashit(get_permalink($id));
- // Make sure fake pages' permalinks are canonical
- if (!empty($current_fp))
- $link .= user_trailingslashit($current_fp);
- echo '<link rel="canonical" href="'.$link.'" />';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement