简体   繁体   中英

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

And what I want is: 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. The result was 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.

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. 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/.

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”? 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? 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/

A good Tutorial about Custom Post Type Permalinks includes using endpoint :

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

Approach on category :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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