首页 文章

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

提问于
浏览
0

我正在创建共享我博客类别的自定义帖子类型 . 我想要的是将类别名称放在永久链接上,并删除自定义帖子类型名称 .

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

我想要的是:www.mywebsite.com/ category / post-name

我试图把register_post_type数组'重写'=>数组('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);

1 回答

  • 0

    我不认为你可以通过重写参数来实现它,这更复杂,你需要使用Endpoint Mask API .

    What are endpoints?

    使用 endpoints 可以轻松创建重写规则以捕获正常的WordPress URL,但最后会有一些额外的 . 例如,您可以使用 endpoints 匹配所有帖子URL,后跟“gallery”,并显示帖子中使用的所有图片,例如example.com/my-fantastic-post/gallery/ .

    使用您自己的自定义重写规则,这样的简单案例相对容易实现 . 然而, endpoints 的力量在更复杂的情况下闪耀 . 如果您想识别以“gallery”结尾的帖子和页面的网址,该怎么办?如果您希望能够捕获多个不同的归档URL,例如日,月,年和类别存档,附加“xml”以输出存档的XML表示形式?对于这些情况, endpoints 非常有用,因为它们允许您通过单个函数调用将字符串添加到多个重写结构的末尾 .

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

    一个好 Tutorial 关于 Custom Post Type Permalinks includes using endpoint

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

    类别方法:

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

相关问题