这让我抓狂,我相信这很简单,但我搜索的东西都没有简单的结构(一切都很复杂)。
我有一个自定义的帖子类型product_listing
和自定义分类法product_cat
(这是分层的,应该有相似的类别)。
我只是希望我的URL如下所示:
mysite.com/products/category1/product-name1
mysite.com/products/category2/product-name2
但就我的一生而言,无论我做什么,我都会遇到令人恐惧的404问题。页面工作正常,帖子工作正常,但我的自定义帖子无法正常工作。他们表现为:
mysite.com/products/product-name1
mysite.com/products/product-name2
这实际上是有效的!我只是想在那里看到我的自定义分类法,而且我想能够访问
taxonomy.php
我已通过转到以下位置设置模板:
mysite.com/products/category1/
mysite.com/products/category2/
我的鼻涕虫都不一样,我也不希望它们是一样的。这是我的
functions.php
文件:
///// CUSTOM POST TYPES /////
// register the new post type
register_post_type( \'product_listing\', array(
\'labels\' => array(
\'name\' => __( \'Products\' ),
\'singular_name\' => __( \'Product\' ),
\'add_new\' => __( \'Add New\' ),
\'add_new_item\' => __( \'Create New Product\' ),
\'edit\' => __( \'Edit\' ),
\'edit_item\' => __( \'Edit Product\' ),
\'new_item\' => __( \'New Product\' ),
\'view\' => __( \'View Products\' ),
\'view_item\' => __( \'View Product\' ),
\'search_items\' => __( \'Search Products\' ),
\'not_found\' => __( \'No products found\' ),
\'not_found_in_trash\' => __( \'No products found in trash\' ),
\'parent\' => __( \'Parent Product\' ),
),
\'description\' => __( \'This is where you can create new products on your site.\' ),
\'public\' => true,
\'show_ui\' => true,
\'capability_type\' => \'post\',
\'publicly_queryable\' => true,
\'exclude_from_search\' => false,
\'menu_position\' => 2,
\'menu_icon\' => get_stylesheet_directory_uri() . \'/images/tag_orange.png\',
\'hierarchical\' => true,
\'_builtin\' => false, // It\'s a custom post type, not built in!
\'rewrite\' => array( \'slug\' => \'products\', \'with_front\' => true ),
\'query_var\' => true,
\'supports\' => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'trackbacks\', \'custom-fields\', \'comments\', \'revisions\' ),
) );
//hook into the init action and call create_book_taxonomies when it fires
add_action( \'init\', \'create_product_taxonomies\', 0 );
//add_action(\'admin_init\', \'flush_rewrite_rules\');
//create two taxonomies, genres and writers for the post type "book"
function create_product_taxonomies() {
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
\'name\' => _x( \'Categories\', \'taxonomy general name\' ),
\'singular_name\' => _x( \'Category\', \'taxonomy singular name\' ),
\'search_items\' => __( \'Search Categories\' ),
\'all_items\' => __( \'All Categories\' ),
\'parent_item\' => __( \'Parent Categories\' ),
\'parent_item_colon\' => __( \'Parent Categories:\' ),
\'edit_item\' => __( \'Edit Category\' ),
\'update_item\' => __( \'Update Category\' ),
\'add_new_item\' => __( \'Add New Category\' ),
\'new_item_name\' => __( \'New Category Name\' ),
\'menu_name\' => __( \'Category\' ),
);
register_taxonomy( \'product_cat\', array( \'product_listing\' ), array(
\'hierarchical\' => true,
\'labels\' => $labels,
\'show_ui\' => true,
\'query_var\' => true,
//\'rewrite\' => true,
\'rewrite\' => array( \'slug\' => \'%category%\', \'with_front\' => true ),
) );
// Add new taxonomy, NOT hierarchical (like tags)
$labels = array(
\'name\' => _x( \'Scents\', \'taxonomy general name\' ),
\'singular_name\' => _x( \'Scent\', \'taxonomy singular name\' ),
\'search_items\' => __( \'Search Scents\' ),
\'popular_items\' => __( \'Popular Scents\' ),
\'all_items\' => __( \'All Scents\' ),
\'parent_item\' => null,
\'parent_item_colon\' => null,
\'edit_item\' => __( \'Edit Scent\' ),
\'update_item\' => __( \'Update Scent\' ),
\'add_new_item\' => __( \'Add New Scent\' ),
\'new_item_name\' => __( \'New Scent Name\' ),
\'separate_items_with_commas\' => __( \'Separate scents with commas\' ),
\'add_or_remove_items\' => __( \'Add or remove scents\' ),
\'choose_from_most_used\' => __( \'Choose from the most used scents\' ),
\'menu_name\' => __( \'Scents\' ),
);
register_taxonomy( \'scent\', \'product_listing\', array(
\'hierarchical\' => false,
\'labels\' => $labels,
\'show_ui\' => true,
\'query_var\' => true,
//\'rewrite\' => array( \'slug\' => \'scents\' ),
) );
}
我还有另一个自定义分类法
scents
理想情况下,我希望有一些友好的url,但我对此持开放态度。我想通过访问
mysite.com/products/scents
但它们不必是特定类别的。
有人能帮我吗?
SO网友:Leo Koo
事实上,这很容易。你只需要一句话。这是我的密码
function create_product_taxonomies()
{
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
\'name\' => _x(\'Categories\', \'taxonomy general name\'),
\'singular_name\' => _x(\'Category\', \'taxonomy singular name\'),
\'search_items\' => __(\'Search Categories\'),
\'all_items\' => __(\'All Categories\'),
\'parent_item\' => __(\'Parent Categories\'),
\'parent_item_colon\' => __(\'Parent Categories:\'),
\'edit_item\' => __(\'Edit Category\'),
\'update_item\' => __(\'Update Category\'),
\'add_new_item\' => __(\'Add New Category\'),
\'new_item_name\' => __(\'New Category Name\'),
\'menu_name\' => __(\'Category\'),
);
register_taxonomy(\'product_cat\', array(\'product_listing\'), array(
\'hierarchical\' => true,
\'labels\' => $labels,
\'show_ui\' => true,
\'query_var\' => true,
\'rewrite\' => array(\'hierarchical\' => true),
));
并应用于GenerateWP生成的我的评论CPT的分类法。com。我在自己的WordPress网站上使用这个,
https://www.wpstarters.com
function reviews_category_taxonomy() {
$labels = array(
\'name\' => _x( \'Reviews Categories\', \'Taxonomy General Name\', \'reviews_category\' ),
\'singular_name\' => _x( \'Reviews Category\', \'Taxonomy Singular Name\', \'reviews_category\' ),
\'menu_name\' => __( \'Reviews Category\', \'reviews_category\' ),
\'all_items\' => __( \'All Review Categories\', \'reviews_category\' ),
\'parent_item\' => __( \'Parent Review Category\', \'reviews_category\' ),
\'parent_item_colon\' => __( \'Parent Review Category:\', \'reviews_category\' ),
\'new_item_name\' => __( \'New Review Category Name\', \'reviews_category\' ),
\'add_new_item\' => __( \'Add New Review Category\', \'reviews_category\' ),
\'edit_item\' => __( \'Edit Review Category\', \'reviews_category\' ),
\'update_item\' => __( \'Update Review Category\', \'reviews_category\' ),
\'view_item\' => __( \'View Review Category\', \'reviews_category\' ),
\'separate_items_with_commas\' => __( \'Separate items with commas\', \'reviews_category\' ),
\'add_or_remove_items\' => __( \'Add or remove items\', \'reviews_category\' ),
\'choose_from_most_used\' => __( \'Choose from the most used\', \'reviews_category\' ),
\'popular_items\' => __( \'Popular Review Categories\', \'reviews_category\' ),
\'search_items\' => __( \'Search Items\', \'reviews_category\' ),
\'not_found\' => __( \'Not Found\', \'reviews_category\' ),
\'no_terms\' => __( \'No Review Categories\', \'reviews_category\' ),
\'items_list\' => __( \'Review Categories list\', \'reviews_category\' ),
\'items_list_navigation\' => __( \'Review Categories list navigation\', \'reviews_category\' ),
);
$args = array(
\'labels\' => $labels,
\'hierarchical\' => true,
\'public\' => true,
\'show_ui\' => true,
\'show_admin_column\' => true,
\'show_in_nav_menus\' => true,
\'show_tagcloud\' => false,
\'show_in_rest\' => true,
\'rewrite\' => array( \'hierarchical\' => true ),
);
register_taxonomy( \'reviews_category\', array( \'wps_reviews\' ), $args );
}
add_action( \'init\', \'reviews_category_taxonomy\', 0 );
你所需要的就是把“重写”=>数组(“分层”=>true),