WP Rewrite Rule Issue bei Verwendung von benutzerdefiniertem Post-Typ und Taxonomie

Ich versuche genau das gleiche zu erreichen, was Milo in diesem Beitrag teilt: WP Rewrite Rules – Custom Post Type & Taxonomy

Alles funktioniert tadellos mit Paginierung, aber ich habe einen 404, während ich versuche, auf meinen einzelnen kundenspezifischen Postartposten zuzugreifen.

Erklären :

site.com/products = OK

site.com/products/page/2 = OK

site.com/products/category = OK

site.com/products/category/page/2 = OK

site.com/products/category/product-name = 404

Hier ist mein Code ($ productCategoryBaseSlug ist eine PHP-Variable, die irgendwo anders gesetzt ist, mit einem Wert von “products” in diesem Beispiel):

/**** CUSTOM POST TYPE ****/ $labels = array( 'name' => __( 'Product', 'Post Type General Name', $this->text_domain ), 'singular_name' => __( 'Product', 'Post Type Singular Name', $this->text_domain ), 'menu_name' => __( 'Products', $this->text_domain ), 'name_admin_bar' => __( 'Product', $this->text_domain ), 'archives' => __( 'Product Archives', $this->text_domain ), 'parent_item_colon' => __( 'Parent Product:', $this->text_domain ), 'all_items' => __( 'All Products', $this->text_domain ), 'add_new_item' => __( 'Add New Product', $this->text_domain ), 'add_new' => __( 'Add New', $this->text_domain ), 'new_item' => __( 'New Product', $this->text_domain ), 'edit_item' => __( 'Edit Product', $this->text_domain ), 'update_item' => __( 'Update Product', $this->text_domain ), 'view_item' => __( 'View Product', $this->text_domain ), 'search_items' => __( 'Search Product', $this->text_domain ), 'not_found' => __( 'Not found', $this->text_domain ), 'not_found_in_trash' => __( 'Not found in Trash', $this->text_domain ), 'featured_image' => __( 'Featured Image', $this->text_domain ), 'set_featured_image' => __( 'Set featured image', $this->text_domain ), 'remove_featured_image' => __( 'Remove featured image', $this->text_domain ), 'use_featured_image' => __( 'Use as featured image', $this->text_domain ), 'insert_into_item' => __( 'Insert into Product', $this->text_domain ), 'uploaded_to_this_item' => __( 'Uploaded to this Product', $this->text_domain ), 'items_list' => __( 'Products list', $this->text_domain ), 'items_list_navigation' => __( 'Products list navigation', $this->text_domain ), 'filter_items_list' => __( 'Filter Products list', $this->text_domain ), ); $args = array( 'label' => __( 'Product', $this->text_domain ), 'description' => __( 'Replacement for text post', $this->text_domain ), 'labels' => $labels, 'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'trackbacks', 'revisions', 'custom-fields', 'page-attributes', 'post-formats', ), 'taxonomies' => array( 'product_category', 'brand'), 'hierarchical' => false, 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'menu_position' => 5, 'show_in_admin_bar' => true, 'show_in_nav_menus' => true, 'can_export' => true, 'has_archive' => $productCategoryBaseSlug, 'exclude_from_search' => false, 'publicly_queryable' => true, 'query_var' => false, 'capability_type' => 'post', 'rewrite' => array( 'slug' => $productCategoryBaseSlug.'%product_category%', 'with_front' => false ) ); register_post_type( 'product', $args ); /**** CUSTOM TAXONOMY ****/ $genre_args = array( 'hierarchical' => true, 'labels' => array( 'name'=> __('Categories', 'taxonomy general name', $this->text_domain), 'singular_name' => __('Category', 'taxonomy singular name', $this->text_domain), 'search_items' => __('Search Categories', $this->text_domain), 'popular_items' => __('Popular Categories', $this->text_domain), 'all_items' => __('All Categories', $this->text_domain), 'edit_item' => __('Edit Category', $this->text_domain), 'update_item' => __('Update Category', $this->text_domain), 'add_new_item' => __('Add New Category', $this->text_domain), 'new_item_name' => __('New Category Name', $this->text_domain), 'separate_items_with_commas' => __('Seperate Categories with Commas', $this->text_domain), 'add_or_remove_items' => __('Add or Remove Categories', $this->text_domain), 'choose_from_most_used' => __('Choose from Most Used Categories', $this->text_domain) ), 'public' => true, 'show_in_nav_menus' => true, 'query_var' => true, 'rewrite' => array( 'with_front' => false, 'slug' => $productCategoryBaseSlug /*__( 'product_category', 'product-category', $this->text_domain )*/ ) ); register_taxonomy('product_category', 'product', $genre_args); /**** REWRITE ****/ add_rewrite_rule( $productCategoryBaseSlug.'/([^/]+)/page/([0-9]+)/?$', 'index.php?product_category=$matches[1]&paged=$matches[2]', 'top' ); /**** CONTENT OF PERMALINK FUNCTION ****/ if ( is_object( $post ) && $post->post_type == 'product' ){ $terms = wp_get_object_terms( $post->ID, 'product_category' ); if( $terms ){ //return str_replace( '%product_category%' , $terms[0]->slug , $post_link ); $post_link = str_replace('%product_category%', get_taxonomy_parents(array_pop($terms)->term_id, 'product_category', false, '/', true), $post_link); } } 

Kann mir jemand dabei helfen? Ich stehe stundenlang fest und finde keine Lösung: /

EDIT: Wie in Kommentar unten erläutert, machte ich einen Fehler in der benutzerdefinierten Post-Typ-Deklaration, ein Schrägstrich fehlte, neuer Code ist:

 'rewrite' => array( 'slug' => $productCategoryBaseSlug.'/%product_category%', 'with_front' => false ) 

Aber jetzt, das Ergebnis ist das Folgende:

site.com/products = OK

site.com/products/page/2 = OK

site.com/products/category = OK

site.com/products/category/page/2 = BROKEN, führt zu site.com/page/2

site.com/products/category/product-name = OK

Ich würde davon leben, Milos Ratschlag ( https://wordpress.stackexchange.com/users/4771/milo ) darüber zu lesen, da er der wahre Experte der Rewrite-Regeln zu sein scheint!

Vielen Dank

Solutions Collecting From Web of "WP Rewrite Rule Issue bei Verwendung von benutzerdefiniertem Post-Typ und Taxonomie"

Ich weiß nicht wirklich warum, aber hier ist eine funktionierende Lösung:

  $blogCategoryBaseSlug = 'articles'; $labels = array( 'name' => __( 'Blog Post', 'Post Type General Name', $this->text_domain ), 'singular_name' => __( 'Blog Post', 'Post Type Singular Name', $this->text_domain ), 'menu_name' => __( 'Blog Posts', $this->text_domain ), 'name_admin_bar' => __( 'Blog Post', $this->text_domain ), 'archives' => __( 'Blog Post Archives', $this->text_domain ), 'parent_item_colon' => __( 'Parent Blog Post:', $this->text_domain ), 'all_items' => __( 'All Blog Posts', $this->text_domain ), 'add_new_item' => __( 'Add Blog Post', $this->text_domain ), 'add_new' => __( 'Add New', $this->text_domain ), 'new_item' => __( 'New Blog Post', $this->text_domain ), 'edit_item' => __( 'Edit Blog Post', $this->text_domain ), 'update_item' => __( 'Update Blog Post', $this->text_domain ), 'view_item' => __( 'View Blog Post', $this->text_domain ), 'search_items' => __( 'Search Blog Post', $this->text_domain ), 'not_found' => __( 'Not found', $this->text_domain ), 'not_found_in_trash' => __( 'Not found in Trash', $this->text_domain ), 'featured_image' => __( 'Featured Image', $this->text_domain ), 'set_featured_image' => __( 'Set featured image', $this->text_domain ), 'remove_featured_image' => __( 'Remove featured image', $this->text_domain ), 'use_featured_image' => __( 'Use as featured image', $this->text_domain ), 'insert_into_item' => __( 'Insert into Blog Post', $this->text_domain ), 'uploaded_to_this_item' => __( 'Uploaded to this Blog Post', $this->text_domain ), 'items_list' => __( 'Blog Posts list', $this->text_domain ), 'items_list_navigation' => __( 'Blog Posts list navigation', $this->text_domain ), 'filter_items_list' => __( 'Filter Blog Posts list', $this->text_domain ), ); $args = array( 'label' => __( 'Blog Post', $this->text_domain ), 'description' => __( 'Replacement for text post', $this->text_domain ), 'labels' => $labels, 'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'trackbacks', 'revisions', 'custom-fields', 'page-attributes', 'post-formats', ), 'taxonomies' => array( 'blog_post_category', 'blog_post_tag'), 'hierarchical' => false, 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'menu_position' => 5, 'show_in_admin_bar' => true, 'show_in_nav_menus' => true, 'can_export' => true, 'has_archive' => $blogCategoryBaseSlug, 'exclude_from_search' => false, 'publicly_queryable' => true, 'capability_type' => 'post', 'rewrite' => array( 'slug' => $blogCategoryBaseSlug.'/%blog_post_category%', 'with_front' => false ) ); register_post_type( 'blogpost', $args ); $genre_args = array( 'labels' => array( 'name'=> __('Categories', 'taxonomy general name', $this->text_domain), 'singular_name' => __('Category', 'taxonomy singular name', $this->text_domain), 'search_items' => __('Search Categories', $this->text_domain), 'popular_items' => __('Popular Categories', $this->text_domain), 'all_items' => __('All Categories', $this->text_domain), 'edit_item' => __('Edit Category', $this->text_domain), 'update_item' => __('Update Category', $this->text_domain), 'add_new_item' => __('Add New Category', $this->text_domain), 'new_item_name' => __('New Category Name', $this->text_domain), 'separate_items_with_commas' => __('Seperate Categories with Commas', $this->text_domain), 'add_or_remove_items' => __('Add or Remove Categories', $this->text_domain), 'choose_from_most_used' => __('Choose from Most Used Categories', $this->text_domain) ), 'public' => true, 'show_in_nav_menus' => true, 'hierarchical' => true, 'query_var' => true, 'rewrite' => array( 'with_front' => false, 'hierarchical' => true, 'slug' => $blogCategoryBaseSlug ) ); register_taxonomy('blog_post_category', 'blogpost', $genre_args); add_rewrite_rule( $blogCategoryBaseSlug.'/([^/]+)/page/([0-9]+)/?$', 'index.php?blog_post_category=$matches[1]&paged=$matches[2]', 'top' ); add_rewrite_rule( $blogCategoryBaseSlug.'/(.+?)/([^/]+)(?:/([0-9]+))?/?$', 'index.php?blog_post_category=$matches[1]&blogpost=$matches[2]&paged=$matches[3]', 'top' ); 

Aber es gibt immer noch ein Problem in Bezug auf die Neufassungsregeln … Bis jetzt funktioniert alles:

site.com/articles= OK

site.com/articles/page/2 = OK

site.com/articles/category = OK

site.com/articles/category/page/2 = OK

site.com/articles/category/article-name = OK

Aber wenn ich eine Unterkategorie erstelle, funktioniert sie nicht:

site.com/articles/category/subcategory = 404 (Suche nach einem Beitrag und nicht nach einer Unterkategorie)

Gibt es eine Lösung in den Rewrite-Regeln, um das zu beheben?