[英]Wordpress Custom Type permalink containing Taxonomy slug
我正在嘗試為自定義類型創建一個永久鏈接模式,其中包括一個分類法。 分類法名稱從一開始就是已知的(所以我不是要添加或混合所有分類法,只是特定的分類法),但當然,價值將是動態的。
通常,自定義類型永久鏈接是使用帶有slug
參數的rewrite
arg構建的,但我不知道如何在那里添加動態變量。
http://codex.wordpress.org/Function_Reference/register_post_type
我猜測需要一個自定義解決方案,但我不確定最好的非侵入式方法是什么。
是否有一個已知的做法或最近有人建造類似的東西? 我正在使用WP 3.2.1 btw。
經過更多搜索后,我設法使用custom_post_link
過濾器創建了相當優雅的解決方案。
假設您有一個帶有client
分類法的project
自定義類型。 添加此鈎子:
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%')
我想在問之前我應該深入挖掘,但至少我們現在有一個完整的解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.