WordPress 自定义永久链接结构:页面、帖子和自定义帖子类型
我正在尝试根据自定义父/子分类法为自定义帖子类型设置自定义永久链接结构。我已经成功实现了这一目标,但我破坏了页面和帖子的永久链接结构。
我已经完成了以下操作:
mysite.com/taxonomy_parent/taxonomy_child/postname
换句话说,我已经删除了分类法,并且 URL 显示了正确的层次结构顺序。首先,我在注册自定义分类法和自定义帖子类型时设置“重写”参数:
对于自定义分类法:
'rewrite' => array( 'slug' => '', 'with_front' => false, 'hierarchical' => true ),
对于帖子类型:
'rewrite' => array( 'slug' => '%mytaxonomy%', 'with_front' => false, 'hierarchical' => true ),
然后我添加了以下代码以获得在中显示父/子分类法的支持网址。
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_rules 有问题,但不知道如何修复它或如何设置新规则来支持我的自定义 post_type/自定义分类永久链接结构,同时帖子和页面的自定义永久链接结构正常工作。
I'm trying to set a custom permalink structure for a custom post type based on a custom parent/child taxonomy. I've managed to achieve the thing but I broke the permalink structure for pages and posts.
I've accomplisged the following:
mysite.com/taxonomy_parent/taxonomy_child/postname
In other words, I've remove the taxonomy slug and the URL show the correct hierarchical order. First, I set the 'rewrite' arg when registering the custom taxonomy and the custom post-type:
for the custom taxonomy:
'rewrite' => array( 'slug' => '', 'with_front' => false, 'hierarchical' => true ),
for the post type:
'rewrite' => array( 'slug' => '%mytaxonomy%', 'with_front' => false, 'hierarchical' => true ),
Then I added the following code in order to get support to show the parent/child taxonomy in the URL.
add_filter('rewrite_rules_array', 'mmp_rewrite_rules');
function mmp_rewrite_rules($rules) {
$newRules = array();
$newRules['(.+)/(.+)/(.+)/?
This works great but there's a conflict with the custom permalink for the posts and pages. I need to configure that too. Actually, I need "mysite.com/blog/category/postname" and "mysite.com/pagename". As easy as to set a custom permalink like that "/%category%/%postname%/" works properly but a 404 error page is returned when the necessary current config I've explained is set in my functions.php
I suppose there's a problem with rewrite_rules but don't know how to fix it or how to set new rules to support my custom post_type/custom taxonomy permalink structure at the same time the custom permalink structure for posts and pages works properly.
] = 'index.php?myposttype=$matches[3]';
$newRules['(.+)/?
This works great but there's a conflict with the custom permalink for the posts and pages. I need to configure that too. Actually, I need "mysite.com/blog/category/postname" and "mysite.com/pagename". As easy as to set a custom permalink like that "/%category%/%postname%/" works properly but a 404 error page is returned when the necessary current config I've explained is set in my functions.php
I suppose there's a problem with rewrite_rules but don't know how to fix it or how to set new rules to support my custom post_type/custom taxonomy permalink structure at the same time the custom permalink structure for posts and pages works properly.
] = '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;
}
This works great but there's a conflict with the custom permalink for the posts and pages. I need to configure that too. Actually, I need "mysite.com/blog/category/postname" and "mysite.com/pagename". As easy as to set a custom permalink like that "/%category%/%postname%/" works properly but a 404 error page is returned when the necessary current config I've explained is set in my functions.php
I suppose there's a problem with rewrite_rules but don't know how to fix it or how to set new rules to support my custom post_type/custom taxonomy permalink structure at the same time the custom permalink structure for posts and pages works properly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论