遵循Jeffs关于this thread 要在permalink中创建嵌套的父分类术语和子分类术语,请执行以下操作。。。
url。com/自定义帖子类型/分类父级/分类子级/cpt条目
除了一个问题外,它基本上都能正常工作。
在继续之前,我应该提到我有一个自定义的post类型,名为gallery 还有一种分类法叫做gallery_cat.
/gallery/ - archive-gallery.php
/gallery/wedding-cakes/ - parent taxonomy - taxonomy-gallery_cat.php
/gallery/wedding-cakes/chocolate/ - child taxonomy - taxonomy-gallery_cat.php
/gallery/wedding-cakes/chocolate/cpt-entry/ - CPT entry under child taxonomy - single-gallery.php
不工作
/gallery/wedding-cakes/cpt-entry/ - CPT entry under parent taxonomy
这是我在函数中的代码。php:
// ADD A GALLERY CUSTOM POST TYPE WITH GALLERY_CAT TAXONOMY
add_action(\'init\', \'create_gallery_post_type\');
function create_gallery_post_type() {
register_taxonomy(
\'gallery_cat\',
\'gallery\',
array(
\'label\' => \'Categories\',
\'singular_label\' => \'Category\',
\'hierarchical\' => true,
\'query_var\' => true,
\'rewrite\' => array(\'slug\' => \'gallery\', \'hierarchical\' => true ),
)
);
$labels = array(
\'name\' => _x(\'Gallery\', \'post type general name\'),
\'singular_name\' => _x(\'Photo\', \'post type singular name\'),
\'add_new\' => _x(\'Add New\', \'gallery\'),
);
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'query_var\' => true,
\'capability_type\' => \'post\',
\'hierarchical\' => false,
\'menu_position\' => null,
\'supports\' => array(\'title\',\'editor\',\'thumbnail\', \'excerpt\', \'author\', \'comments\'),
\'rewrite\' => array(
\'slug\' => \'gallery/%gallery_cat%\',
\'with_front\' => false
),
\'has_archive\' => \'gallery\',
\'menu_icon\' => get_bloginfo(\'template_directory\') . \'/images/gallery-icon.png\' //16x16 png if you want an icon
);
register_post_type( \'gallery\' , $args );
}
function filter_post_type_link($link, $post) {
if ($post->post_type != \'gallery\')
return $link;
if ($cats = get_the_terms($post->ID, \'gallery_cat\')) {
$link = str_replace(\'%gallery_cat%\', rtrim(get_taxonomy_parents(array_pop($cats)->term_id, \'gallery_cat\', false, \'/\', true),"/"), $link); // see custom function defined below
}
return $link;
}
add_filter(\'post_type_link\', \'filter_post_type_link\', 10, 2);
// my own function to do what get_category_parents does for other taxonomies
function get_taxonomy_parents($id, $taxonomy, $link = false, $separator = \'/\', $nicename = false, $visited = array()) {
$chain = \'\';
$parent = &get_term($id, $taxonomy);
if (is_wp_error($parent)) {
return $parent;
}
if ($nicename)
$name = $parent -> slug;
else
$name = $parent -> name;
if ($parent -> parent && ($parent -> parent != $parent -> term_id) && !in_array($parent -> parent, $visited)) {
$visited[] = $parent -> parent;
$chain .= get_taxonomy_parents($parent -> parent, $taxonomy, $link, $separator, $nicename, $visited);
}
if ($link) {
// nothing, can\'t get this working :(
} else
$chain .= $name . $separator;
return $chain;
}
//Rewrite rules
add_filter(\'rewrite_rules_array\', \'gallery_rewrite_rules\');
function gallery_rewrite_rules($rules) {
$newRules = array();
$newRules[\'gallery/(.+)/(.+)/(.+)/?$\'] = \'index.php?gallery=$matches[3]\';
$newRules[\'gallery/(.+)/(.+)/?$\'] = \'index.php?gallery_cat=$matches[2]\';
$newRules[\'gallery/(.+)/?$\'] = \'index.php?gallery_cat=$matches[1]\';
return array_merge($newRules, $rules);
}
现在,我认为问题在于我需要在重写规则数组中添加以下行:
$newRules[\'gallery/(.+)/(.+)/?$\'] = \'index.php?gallery=$matches[2]\';
因为我的自定义结构不会总是将帖子名作为第三个uri段,因为如果CPT仅在父分类下,那么它将位于第二个uri段中。
但当我加上这一点时,它破坏了一切。我最好的猜测是它与这条线冲突。。。
$newRules[\'gallery/(.+)/(.+)/?$\'] = \'index.php?gallery_cat=$matches[2]\';
杰夫,是谁帮我做到了这一点
this thread, 说了下面的话。。。
如果将有不同级别的嵌套术语,那么需要编写一个函数来检查最后一个uri段是自定义post类型还是分类术语,以了解要添加的规则
有人知道如何做到这一点吗?
谢谢