![](/img/trans.png)
[英]How to get permalink of custom post type that filter by category in Wordpress?
[英]How to put the category on the permalink of a custom post type
我正在創建共享博客類別的自定義帖子類型。 我想要的是將類別名稱放在永久鏈接上,並刪除自定義帖子類型名稱。
現在我有:www.mywebsite.com/ custom-post-type-name / post-name
我想要的是:www.mywebsite.com/ category / post-name
我試圖穿上register_post_type數組'rewrite'=> array('slug'=>'%category%'),但是它不起作用。 結果是www.mywebsite.com/%category%/post-name
先感謝您! (對不起我的英語)
$labels = array(
'name' => _x('Whitepaper', 'post type general name'),
'singular_name' => _x('Whitepaper', 'post type singular name'),
'add_new' => _x('Add New', 'Whitepaper'),
'add_new_item' => __('Add New Whitepaper'),
'edit_item' => __('Edit Whitepaper'),
'new_item' => __('New Whitepaper'),
'all_items' => __('All Whitepapers'),
'view_item' => __('View Whitepaper'),
'search_items' => __('Search Whitepapers'),
'not_found' => __('No Whitepapers found'),
'not_found_in_trash' => __('No Whitepapers found in Trash'),
'parent_item_colon' => '',
'menu_name' => 'Whitepapers'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => true,
'menu_position' => null,
'supports' => array('title','editor','author','thumbnail','excerpt','comments','custom-fields'),
'taxonomies' => array('category'),
'rewrite' => array('slug' => '%category%')
);
register_post_type('whitepaper',$args);
我認為您無法通過rewrite參數來做到這一點,這更加復雜,您需要使用Endpoint Mask API來實現。
什么是端點?
使用端點使您可以輕松創建重寫規則以捕獲正常的WordPress URL,但最后還有一些額外的內容。 例如,您可以使用一個端點來匹配所有帖子URL,后跟“圖庫”,並顯示該帖子中使用的所有圖像,例如example.com/my-fantastic-post/gallery/。
使用您自己的自定義重寫規則,相對容易實現這樣的簡單情況。 但是,端點的功能適用於更復雜的情況。 如果您想識別以“圖庫”結尾的帖子和頁面的URL,該怎么辦? 如果您希望能夠捕獲多個不同的存檔URL,例如日,月,年和類別存檔,並添加“ xml”以輸出存檔的XML表示,該怎么辦? 在這些情況下,端點非常有用,因為它們允許您通過一個函數調用將字符串添加到多個重寫結構的末尾。
資料來源:make.wordpress.org/plugins/2012/06/07/rewrite-endpoints-api/
關於自定義帖子類型永久鏈接的一個很好的教程 包括使用端點 :
[第2部分] http://shibashake.com/wordpress-theme/custom-post-type-permalinks-part-2
分類方法:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.