WordPress自定义永久链接结构:页面、帖子和自定义帖子类型
我正在尝试为基于自定义父/子分类法的自定义帖子类型设置自定义permalink结构。我成功地实现了这一目标,但我打破了页面和帖子的永久链接结构 我已完成以下工作: mysite.com/taxonomy\u parent/taxonomy\u child/postname 换句话说,我已经删除了分类法slug,URL显示了正确的层次顺序。首先,我在注册自定义分类法和自定义帖子类型时设置了“rewrite”参数: 对于自定义分类法:WordPress自定义永久链接结构:页面、帖子和自定义帖子类型,wordpress,rewrite,custom-post-type,permalinks,custom-taxonomy,Wordpress,Rewrite,Custom Post Type,Permalinks,Custom Taxonomy,我正在尝试为基于自定义父/子分类法的自定义帖子类型设置自定义permalink结构。我成功地实现了这一目标,但我打破了页面和帖子的永久链接结构 我已完成以下工作: mysite.com/taxonomy\u parent/taxonomy\u child/postname 换句话说,我已经删除了分类法slug,URL显示了正确的层次顺序。首先,我在注册自定义分类法和自定义帖子类型时设置了“rewrite”参数: 对于自定义分类法: 'rewrite' => array( 'slug' =&
'rewrite' => array( 'slug' => '', 'with_front' => false, 'hierarchical' => true ),
对于职位类型:
'rewrite' => array( 'slug' => '%mytaxonomy%', 'with_front' => false, 'hierarchical' => true ),
然后我添加了以下代码,以获得在URL中显示父/子分类法的支持
add_filter('rewrite_rules_array', 'mmp_rewrite_rules');
function mmp_rewrite_rules($rules) {
$newRules = array();
$newRules['(.+)/(.+)/(.+)/?$'] = 'index.php?myposttype=$matches[3]';
$newRules['(.+)/?$'] = 'index.php?mytaxonomy=$matches[1]';
return array_merge($newRules, $rules);
}
function filter_post_type_link($link, $post)
{
if ($post->post_type != 'myposttype')
return $link;
if ($cats = get_the_terms($post->ID, 'mytaxonomy'))
{
$link = str_replace('%mytaxonomy%', get_taxonomy_parents(array_pop($cats)->term_id, 'mytaxonomy', false, '/', true), $link);
}
return $link;
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);
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;
}
这非常有效,但与帖子和页面的自定义永久链接存在冲突。我也需要配置它。实际上,我需要“mysite.com/blog/category/postname”和“mysite.com/pagename”。设置像“/%category%/%postname%/”这样的自定义永久链接非常简单,但当在我的functions.php中设置了我解释过的必要的当前配置时,会返回404错误页面
我想rewrite_规则有问题,但不知道如何修复它,也不知道如何设置新的规则来支持我的自定义post_类型/自定义分类permalink结构,同时用于post和pages的自定义permalink结构可以正常工作