我现在正在搜索如何将分类法添加到我的自定义帖子类型固定链接的答案 . 我've found this article which almost exactly gives the answer but it doesn' t适用于我的自定义帖子类型 . http://shibashake.com/wordpress-theme/add-custom-taxonomy-tags-to-your-wordpress-permalinks
本文描述了您首先制作一个简单的分类法:
add_action('init', 'my_rating_init');
function my_rating_init() {
if (!is_taxonomy('rating')) {
register_taxonomy( 'rating', 'post',
array( 'hierarchical' => FALSE, 'label' => __('Rating'),
'public' => TRUE, 'show_ui' => TRUE,
'query_var' => 'rating',
'rewrite' => true ) );
}
}
然后你可以在你的Wordpress系统中重写网址,如:/%rating%/%postname%
然后你需要通过执行以下操作将%rating%转换为分类标签:
add_filter('post_link', 'rating_permalink', 10, 3);
add_filter('post_type_link', 'rating_permalink', 10, 3);
function rating_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, '%rating%') === FALSE) return $permalink;
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;
// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, 'rating');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
else $taxonomy_slug = 'not-rated';
return str_replace('%rating%', $taxonomy_slug, $permalink);
}
这适用于“发布”,但是当我改变时:
register_taxonomy( 'rating', 'post',
至:
register_taxonomy( 'rating', 'mycustomposttype',
URL重写不再起作用 . 并且只提供以下网址:
http://www.website.com/custom-post-type/post
我想要的地方:
http://www.website.com/custom-post-type/taxonomy-tag/post
所以我的两个问题是:
-
如何使我的自定义帖子类型工作?
-
如何仅针对我的自定义帖子类型进行此操作?因为我需要将%rating%添加到我的wordpress系统(设置 - >永久链接),所以它会更改我的所有URL .
1 回答
试试这样吧
然后你必须添加自定义URL重写 . 正如所解释here on wordpress site