我正在尝试为“自定义类型”创建一个永久链接模式,其中包括其分类法之一。从一开始就知道分类法的名称(因此,我并不是试图添加或混合其所有分类法,而只是一个特定的分类法),但是该值当然是动态的。
通常,“自定义类型”永久链接是使用带有rewrite
参数的slug
arg构建的,但是我看不到如何在其中添加动态变量。
http://codex.wordpress.org/Function_Reference/register_post_type
我猜想需要自定义解决方案,但是我不确定最好的非侵入性方法是什么。
是否有已知的做法,或者最近有人建立了类似的做法?我正在使用WP 3.2.1 btw。
最佳答案
经过更多搜索之后,我设法使用custom_post_link
过滤器创建了一个非常优雅的解决方案。
假设您有一个project
自定义类型和client
分类法。添加此钩子(Hook):
function custom_post_link($post_link, $id = 0)
{
$post = get_post($id);
if(!is_object($post) || $post->post_type != 'project')
{
return $post_link;
}
$client = 'misc';
if($terms = wp_get_object_terms($post->ID, 'client'))
{
$client = $terms[0]->slug;
//Replace the query var surrounded by % with the slug of
//the first taxonomy it belongs to.
return str_replace('%client%', $client, $post_link);
}
//If all else fails, just return the $post_link.
return $post_link;
}
add_filter('post_type_link', 'custom_post_link', 1, 3);
然后,在注册自定义类型时,按如下所示设置
rewrite
arg:'rewrite' => array('slug' => '%client%')
我想我应该在询问之前进行更深入的研究,但至少现在我们有一个完整的解决方案。
关于wordpress - 包含分类标准的Wordpress自定义类型永久链接,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7723457/