admin管理员组文章数量:1333404
I have a hierarchical taxonomy, county, which takes a parent and child taxonomy as well as a rewrite rule to include them in my permalinks.
I have three of these county taxonomies, but only two of them are working as expected. For the third one, the permalinks break when you add both the parent and child taxonomy, but work correctly if you only add the child taxonomy. If you add both parent and child taxonomy to this specific taxonomy, only the parent shows up in the permalink. I've narrowed it down to it being an issue with that taxonomy's specific child taxonomy, because I added a test child taxonomy and that one worked fine as well.
What's weird is that for the broken permalinks, if you manually add the child taxonomy to the permalink in the address bar, it will take you to the correct page. However, the permalinks created are 404s
What are the potential reasons for rewrite rules not working on a specific child taxonomy? My only guess is some leftover data from when I maybe created a county by the same name before my current setup, but I don't know where to look in the database for that sort of thing.
For reference, my rewrite functions:
<?php
add_filter('rewrite_rules_array', 'mmp_rewrite_rules');
function mmp_rewrite_rules($rules)
{
$newRules = array();
$newRules['floorplans/(.+)/(.+)/(.+)/?$'] = 'index.php?floorplan=$matches[3]';
$newRules['ready-made/(.+)/(.+)/(.+)/?$'] = 'index.php?ready_made=$matches[3]';
$newRules['floorplans/(.+)/(.+)/?$'] = 'index.php?county=$matches[2]';
$newRules['ready-made/(.+)/(.+)/?$'] = 'index.php?county=$matches[2]';
$newRules['floorplans/(.+)/?$'] = 'index.php?county=$matches[1]';
$newRules['ready-made/(.+)/?$'] = 'index.php?county=$matches[1]';
return array_merge($newRules, $rules);
}
function filter_post_type_link($link, $post)
{
$commcheck = get_the_terms($post->ID, 'county');
if ($post->post_type == 'community') {
$link = str_replace('%county%', array_pop($commcheck)->slug, $link);
}
if ($post->post_type == 'floorplan' || $post->post_type == 'ready_made') {
if ($cats = get_the_terms($post->ID, 'county')) {
$link = str_replace('%county%', get_taxonomy_parents(array_pop($cats)->term_id, 'county', 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 = true, $visited = array())
{
$chain = '';
$parentref = get_term($id, $taxonomy);
$parent = &$parentref;
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;
}
本文标签: custom taxonomyRewrite Rule Working for all but one of the taxonomies created
版权声明:本文标题:custom taxonomy - Rewrite Rule Working for all but one of the taxonomies created 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742248715a2440344.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论