我正试图实现米洛在这篇文章中分享的完全相同的东西:WP Rewrite Rules - Custom post type & taxonomy
所有的工作都与分页完美结合,但我在尝试访问我的单一自定义帖子类型帖子时得到了404。
要解释:
地点com/产品=正常
地点com/products/page/2=正常
地点com/产品/类别=正常
地点com/产品/类别/页面/2=正常
地点com/产品/类别/产品名称=404
下面是我的代码($ProductCategoryBaseSlaug是一个php变量集,在本例中的值为“products”):
/**** 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);
}
}
有人能帮我一下吗?我被困了几个小时,找不到解决方案:/
编辑:如下面的注释所述,我在自定义帖子类型声明中出错,缺少斜杠,新代码为:
\'rewrite\' => array(
\'slug\' => $productCategoryBaseSlug.\'/%product_category%\',
\'with_front\' => false
)
但现在,结果如下:
地点com/产品=正常
地点com/products/page/2=正常
地点com/产品/类别=正常
地点com/products/category/page/2=已断开,指向站点。com/page/2
地点com/产品/类别/产品名称=确定
我愿意接受米洛的建议(https://wordpress.stackexchange.com/users/4771/milo) 他似乎是重写规则的真正专家!
非常感谢。
SO网友:Amandine
我真的不知道为什么,但这里有一个可行的解决方案:
$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\'
);
但是关于重写规则还有一个问题。。。到目前为止,一切正常:
地点com/文章=正常
地点com/articles/page/2=正常
地点com/文章/类别=正常
地点com/articles/category/page/2=正常
地点com/文章/类别/文章名称=确定
但如果我创建一个子类别,它就不起作用了:
地点com/articles/category/subcategory=404(寻找帖子而不是子类别)
重写规则中是否有任何解决方案可以解决此问题?