【发布时间】:2014-02-12 08:03:39
【问题描述】:
我现在正在寻找如何将分类法添加到我的自定义帖子类型永久链接的答案。我发现这篇文章几乎完全给出了答案,但它不适用于我的自定义帖子类型。 http://shibashake.com/wordpress-theme/add-custom-taxonomy-tags-to-your-wordpress-permalinks
文章描述你先做一个简单的Taxonomy:
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 系统中重写 url,例如:/%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。
【问题讨论】:
标签: wordpress url-rewriting taxonomy