简体   繁体   English

如何将类别放在自定义帖子类型的永久链接上

[英]How to put the category on the permalink of a custom post type

I'm creating custom post types that are sharing the categories of my blog. 我正在创建共享博客类别的自定义帖子类型。 And what I want is to put the category name on the permalink and also delete the custom post type name. 我想要的是将类别名称放在永久链接上,并删除自定义帖子类型名称。

Now I have: www.mywebsite.com/ custom-post-type-name /post-name 现在我有:www.mywebsite.com/ custom-post-type-name / post-name

And what I want is: www.mywebsite.com/ category /post-name 我想要的是:www.mywebsite.com/ category / post-name

I tried to put on the register_post_type array 'rewrite' => array('slug' => '%category%') but it doesn't works. 我试图穿上register_post_type数组'rewrite'=> array('slug'=>'%category%'),但是它不起作用。 The result was www.mywebsite.com/%category%/post-name 结果是www.mywebsite.com/%category%/post-name

Thank you in advance! 先感谢您! (And sorry for my english) (对不起我的英语)


$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);

I don't think you can make it through rewrite argument, that's more complex and you need to do it with Endpoint Mask API. 我认为您无法通过rewrite参数来做到这一点,这更加复杂,您需要使用Endpoint Mask API来实现。

What are endpoints? 什么是端点?

Using endpoints allows you to easily create rewrite rules to catch the normal WordPress URLs, but with a little extra at the end. 使用端点使您可以轻松创建重写规则以捕获正常的WordPress URL,但最后还有一些额外的内容。 For example, you could use an endpoint to match all post URLs followed by “gallery” and display all of the images used in a post, eg example.com/my-fantastic-post/gallery/. 例如,您可以使用一个端点来匹配所有帖子URL,后跟“图库”,并显示该帖子中使用的所有图像,例如example.com/my-fantastic-post/gallery/。

A simple case like this is relatively easy to achieve with your own custom rewrite rules. 使用您自己的自定义重写规则,相对容易实现这样的简单情况。 However, the power of endpoints shines for more complex situations. 但是,端点的功能适用于更复杂的情况。 What if you wanted to recognise URLs for posts and pages ending with “gallery”? 如果您想识别以“图库”结尾的帖子和页面的URL,该怎么办? What if you wanted to be able to catch multiple different archive URLs, eg day, month, year and category archives, with “xml” appended in order to output an XML representation of the archive? 如果您希望能够捕获多个不同的存档URL,例如日,月,年和类别存档,并添加“ xml”以输出存档的XML表示,该怎么办? For these situations endpoints are very useful as they allow you to add a string to the end of multiple rewrite structures with a single function call. 在这些情况下,端点非常有用,因为它们允许您通过一个函数调用将字符串添加到多个重写结构的末尾。

Source : make.wordpress.org/plugins/2012/06/07/rewrite-endpoints-api/ 资料来源:make.wordpress.org/plugins/2012/06/07/rewrite-endpoints-api/

A good Tutorial about Custom Post Type Permalinks includes using endpoint : 关于自定义帖子类型永久链接的一个很好的教程 包括使用端点

[Part 2] http://shibashake.com/wordpress-theme/custom-post-type-permalinks-part-2 [第2部分] http://shibashake.com/wordpress-theme/custom-post-type-permalinks-part-2

Approach on category : 分类方法:

https://core.trac.wordpress.org/ticket/19275 https://core.trac.wordpress.org/ticket/19275

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM