codememo

분류법 슬러그가 포함된 Wordpress 사용자 지정 유형 퍼멀링크

tipmemo 2023. 6. 27. 22:15
반응형

분류법 슬러그가 포함된 Wordpress 사용자 지정 유형 퍼멀링크

분류법 중 하나를 포함하는 사용자 정의 유형에 대한 영구 링크 패턴을 만들려고 합니다.분류법 이름은 처음부터 알려져 있습니다(그래서 저는 모든 분류법을 추가하거나 혼합하려는 것이 아닙니다, 단지 특정 분류법일 뿐입니다). 하지만 물론 그 가치는 역동적일 것입니다.

일반적으로 사용자 지정 유형 퍼멀 링크는 다음을 사용하여 작성됩니다.rewrite에 대해 논쟁하는.slug매개 변수입니다. 하지만 동적 변수를 어떻게 추가할 수 있는지 모르겠습니다.

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

맞춤형 솔루션이 필요할 것으로 예상되지만, 방해가 되지 않는 최선의 방법은 무엇인지 잘 모르겠습니다.

이것에 대해 알려진 관행이 있습니까? 아니면 최근에 비슷한 것을 만든 사람이 있습니까?저는 WP 3.2.1 btw를 사용하고 있습니다.

좀 더 검색한 후에 저는 꽤 우아한 솔루션을 만들 수 있었습니다.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);

그런 다음 Custom Type을 등록할 때 다음과 같이 설정합니다.rewrite다음과 같은 인수:

'rewrite' => array('slug' => '%client%')

묻기 전에 더 깊이 파고 들었어야 했는데, 적어도 지금은 완전한 해결책이 있습니다.

언급URL : https://stackoverflow.com/questions/7723457/wordpress-custom-type-permalink-containing-taxonomy-slug

반응형