Wordpress自定义类型永久链接包含分类法slug

tre*_*nik 6 wordpress permalinks taxonomy custom-type

我正在尝试为自定义类型创建一个永久链接模式,其中包括一个分类法.分类法名称从一开始就是已知的(所以我不是要添加或混合所有的分类法,只是特定的分类法),但当然,价值将是动态的.

通常,自定义类型永久链接是使用rewritearg和slugparam 构建的,但我不知道如何在其中添加动态变量.

http://codex.wordpress.org/Function_Reference/register_post_type

我猜测需要一个自定义解决方案,但我不确定最好的非侵入式方法是什么.

是否有一个已知的做法或最近有人建造类似的东西?我正在使用WP 3.2.1 btw.

tre*_*nik 4

经过更多搜索后,我设法使用custom_post_link过滤器创建了相当优雅的解决方案。

假设您有一个project带有分类的自定义类型client。添加这个钩子:

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);
Run Code Online (Sandbox Code Playgroud)

然后,在注册自定义类型时,设置rewritearg,如下所示:

'rewrite' => array('slug' => '%client%')
Run Code Online (Sandbox Code Playgroud)

我想我应该在提问之前深入挖掘,但至少我们现在有一个完整的解决方案。