lundi 2 février 2015

404 when remove Slug base custom taxonomy and custom post type


I have a custom taxonomy (cities) and a custom post type (spots). I have removed the custom taxonomy base slug and the custom post type base slug to have this :




  • mysite.com/amsterdam (all spots in amsterdam)




  • http://ift.tt/1zuX53M (a spot in amsterdam)




I have also do a custom permalink for all posts to have a blog (in option permalinks) : http://ift.tt/1Ajri8N


Today, custom taxonomy and custom posts urls works but i have 404 errors for all my blog posts and pages.


Here is my custom function.php:



/**
* Register a custom post type SPOTS
*/
function custom_post_type_spots() {

$labels = array(
'name' => _x( 'Spots', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Spot', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Spots', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'all_items' => __( 'All Items', 'text_domain' ),
'view_item' => __( 'View Item', 'text_domain' ),
'add_new_item' => __( 'Add New Item', 'text_domain' ),
'add_new' => __( 'Add New', 'text_domain' ),
'edit_item' => __( 'Edit Item', 'text_domain' ),
'update_item' => __( 'Update Item', 'text_domain' ),
'search_items' => __( 'Search Item', 'text_domain' ),
'not_found' => __( 'Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ),
);
$rewrite = array(
'slug' => '%cities%',
'with_front' => false,
'pages' => false,
'feeds' => false,
);
$args = array(
'label' => __( 'spots', 'text_domain' ),
'description' => __( 'All Spots', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'trackbacks', 'revisions', 'custom-fields', ),
'taxonomies' => array( 'cities' ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'menu_icon' => '',
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => $rewrite,
'capability_type' => 'post',
);
register_post_type( 'spots', $args );
}
add_action( 'init', 'custom_post_type_spots', 0 );


/**
* Register Custom Taxonomy CITIES
*/
function custom_taxonomy_cities() {
$labels = array(
'name' => _x( 'Cities', 'Taxonomy General Name', 'text_domain' ),
'singular_name' => _x( 'City', 'Taxonomy Singular Name', 'text_domain' ),
'menu_name' => __( 'Cities', 'text_domain' ),
'all_items' => __( 'All Cities', 'text_domain' ),
'parent_item' => __( 'Parent City', 'text_domain' ),
'parent_item_colon' => __( 'Parent City:', 'text_domain' ),
'new_item_name' => __( 'New City Name', 'text_domain' ),
'add_new_item' => __( 'Add New City', 'text_domain' ),
'edit_item' => __( 'Edit City', 'text_domain' ),
'update_item' => __( 'Update City', 'text_domain' ),
'separate_items_with_commas' => __( 'Separate Cities with commas', 'text_domain' ),
'search_items' => __( 'Search Cities', 'text_domain' ),
'add_or_remove_items' => __( 'Add or Remove Cities', 'text_domain' ),
'choose_from_most_used' => __( 'Choose from Most Used Cities', 'text_domain' ),
);

$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => false,
'rewrite' => array('slug' => 'cities'),
);

register_taxonomy( 'cities', 'spots', $args );
}
add_action( 'init', 'custom_taxonomy_cities', 0 );

/**
* Remove the slug from published post permalinks.
*/
function spots_remove_cpt_slug( $post_link, $post, $leavename ) {
if ( 'spots' != $post->post_type || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
return $post_link;
}
add_filter( 'post_type_link', 'spots_remove_cpt_slug', 10, 3 );


function custom_parse_request_tricksy( $query ) {
// Only noop the main query
if ( ! $query->is_main_query() )
return;

// Only noop our very specific rewrite rule match
if ( 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}

// 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'post', 'spots', 'page' ) );
}
}
add_action( 'pre_get_posts', 'custom_parse_request_tricksy' );

function wpa_spots_post_link( $post_link, $id = 0 ){
$post = get_post($id);
if ($post->post_type != 'spots')
return $post_link;

if ( is_object( $post ) ){
$terms = wp_get_object_terms( $post->ID, 'cities' );
if( $terms ){
return str_replace( '%cities%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'wpa_spots_post_link', 1, 3 );




Aucun commentaire:

Enregistrer un commentaire