커스텀 퍼머링크 구조: /% custom-post-type%/% custom-taxonomy%/%post-name%/
다음 작업을 수행할 수 있는 커스텀 퍼머링크 구조를 작성하려고 합니다.
- '프로젝트'라는 커스텀 투고 타입이 있습니다.
- CPT "프로젝트"에 할당된 "프로젝트 카테고리"라는 커스텀 분류법이 있습니다.
내 퍼멀링크 구조를 다음과 같이 만들고 싶다.
프로젝트/카테고리/프로젝트명
또는
/%custom-post-type%/%custom-post-name%/
일반 WP 투고에서는 /%category%/를 정상적으로 사용할 수 있었지만 CPT에서는 사용할 수 없었습니다.
이러한 permalink 구조를 작성하면 URL 또는 다른 페이지에 어떤 영향을 미칩니까?커스텀 퍼멀링크 구조를 정의하고 단일 CPT로 제한할 수 있습니까?
감사해요.
운이 좋으시네요, 고객 프로젝트 때문에 이 일을 해야 했어요.WordPress Stackexchange에서 이 답변을 가이드로 사용했습니다.
/**
* Tell WordPress how to interpret our project URL structure
*
* @param array $rules Existing rewrite rules
* @return array
*/
function so23698827_add_rewrite_rules( $rules ) {
$new = array();
$new['projects/([^/]+)/(.+)/?$'] = 'index.php?cpt_project=$matches[2]';
$new['projects/(.+)/?$'] = 'index.php?cpt_project_category=$matches[1]';
return array_merge( $new, $rules ); // Ensure our rules come first
}
add_filter( 'rewrite_rules_array', 'so23698827_add_rewrite_rules' );
/**
* Handle the '%project_category%' URL placeholder
*
* @param str $link The link to the post
* @param WP_Post object $post The post object
* @return str
*/
function so23698827_filter_post_type_link( $link, $post ) {
if ( $post->post_type == 'cpt_project' ) {
if ( $cats = get_the_terms( $post->ID, 'cpt_project_category' ) ) {
$link = str_replace( '%project_category%', current( $cats )->slug, $link );
}
}
return $link;
}
add_filter( 'post_type_link', 'so23698827_filter_post_type_link', 10, 2 );
커스텀 투고 타입과 분류법을 등록할 때는, 다음의 설정을 사용해 주세요.
// Used for registering cpt_project custom post type
$post_type_args = array(
'rewrite' => array(
'slug' => 'projects/%project_category%',
'with_front' => true
)
);
// Some of the args being passed to register_taxonomy() for 'cpt_project_category'
$taxonomy_args = array(
'rewrite' => array(
'slug' => 'projects',
'with_front' => true
)
);
물론 작업이 끝나면 다시 쓰기 규칙을 반드시 수정하십시오.행운을 빕니다.
최근 워드프레스가 많이 바뀌었기 때문에 이에 대한 새로운 솔루션이 있습니다.
// Used for registering cpt_project custom post type
$post_type_args = array(
'rewrite' => array(
'slug' => '/%custom-post-type%/%custom-taxonomy%/%postname%/',
'with_front' => true
'walk_dirs' => false
)
);
%custom-post-type%는 커스텀 포스트 타입의 이름과 일치해야 합니다.%custom-taxonomy%는 WordPress가 자동으로 올바른 개서 규칙과 링크를 생성하는 분류법 이름과 일치해야 합니다.
'walk_posts' => false를 사용하면 링크가 커스텀 포스트 타입으로 시작되기 때문에 WP가 [^/]+/와 같은 엉뚱한 규칙을 만드는 것을 방지할 수 있습니다.
그리고 종종 이 도보여행은 필요하지 않습니다. 왜냐하면 당신은 당신의 구조 내 사이트나 별도의 분류법 사이트에만 접속하기 때문입니다.
이를 통해 개서 규칙은 가능한 한 정확하며 규칙을 가져올 필요가 없습니다.
add_filter( 'rewrite_rules_array', 'so23698827_add_rewrite_rules' );
나중에 그 앞에 붙여서
add_filter( 'post_type_link', 'so23698827_filter_post_type_link', 10, 2 );
승인된 답변에 언급된 바와 같이.이렇게 하면 메모리와 실행 시간이 절약됩니다.
이 WP 버전 문제를 찾고 있는 모든 사용자에게 도움이 되기를 바랍니다> 5.X
언급URL : https://stackoverflow.com/questions/23698827/custom-permalink-structure-custom-post-type-custom-taxonomy-post-name
'sourcetip' 카테고리의 다른 글
기능 컴포넌트가 있는 컨스트럭터를 지정하는 방법(팻 화살표 구문) (0) | 2023.03.14 |
---|---|
MySQL 데이터베이스에서 WordPress 버전 확인 (0) | 2023.03.14 |
하드코드된 JSON 문자열을 변수에 저장 (0) | 2023.03.14 |
오브젝트 배열에 대해 ng-interface를 설정합니다. (0) | 2023.03.14 |
기록 사용, 리액트 큐브 돔 v6 (0) | 2023.03.14 |