問題描述
這是驅動我堅果,我確信這很簡單,但我沒有找到一個簡單的結構 (一切都非常複雜) 。
我有一個自定義的帖子類型 product_listing 和 product_cat 的定製分類 (它是分級的,應該有類別) 。
我只是希望我的網址如下所示:
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/
我的 s 都不一樣,也不想讓他們成為。以下是我的 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 訪問所有香水的清單,但不一定要分類。
有誰能夠幫助我?
最佳解決方案
將 products/%product_cat% 中的 products/%product_cat% 的 slug 和您的分類學參數中的 slug 更改為 products,然後刷新重寫規則。 WordPress 現在應該處理/products/my-product-cat/post-name/!
現在最後,我們需要幫助 WordPress 生成永久鏈接 (開箱即用,不會識別 Permastruct 標籤%product_cat%):
/**
* Inject term slug into custom post type permastruct.
*
* @link http://wordpress.stackexchange.com/a/5313/1685
*
* @param string $link
* @param WP_Post $post
* @return array
*/
function wpse_5308_post_type_link( $link, $post ) {
if ( $post->post_type === 'product_listing' ) {
if ( $terms = get_the_terms( $post->ID, 'product_cat' ) )
$link = str_replace( '%product_cat%', current( $terms )->slug, $link );
}
return $link;
}
add_filter( 'post_type_link', 'wpse_5308_post_type_link', 10, 2 );
有一件事要注意,這隻會抓住按名稱排列的第一個產品類別。如果您將多個類別分配給單個產品,我可以輕鬆地更改如何確定在固定鏈接中使用哪一個。
Lemme 知道你如何得到這個,我們可以解決其他的問題!
次佳解決方案
感謝 @TheDeadMechanic,你的答案幫助我,但只是部分。我想做同樣的事情 @RodeoRamsey 要求,但嵌套類別 (即:mysite.com/products/category1/child-category-1/grandchild-category-1/product-name),您的解決方案不起作用。
我終於想出了一個擴展的解決方案,我的問題的工作,所以如果有人需要嵌套類別/子類別,你可以看到一個 detailed solution 在我自己的問題。希望它幫助別人,並感謝初步的步驟。
第三種解決方案
我不確定 wp 是否支持開箱即用的結構,但您可以輕鬆地創建自己的重寫規則。
在這裏查看一個以前的答案 Author url rewrite 。
您可以更改行
$newrules['author/([^/]+)/songs/?$'] = 'index.php?post_type=songs&author=$matches[1]';
像一樣
$newrules['products/([^/]+)/([^/]+)/?$'] = 'index.php?post_type=product_listing&product_cat=$matches[1]&name=$matches[2]';
這裏的 product_cat 部分可能是多餘的 – 我不知道是否需要。
您可以添加任何您喜歡的規則,並且它們將優先於內置的規則。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。