[英]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.