programing

사용자 지정 게시물 유형 슬러그 충돌

oldcodes 2023. 6. 13. 22:47
반응형

사용자 지정 게시물 유형 슬러그 충돌

사용자 지정 분류법을 사용하는 여러 사용자 지정 게시물 유형이 있습니다.저는 부모님이 다른데도 심한 충돌을 겪고 있어요.

다음은 제 URL 구조입니다. /work/%client_name%/%project_name%

"/work/client1/some-cool-project-name"이라는 슬러그를 생성하는 클라이언트(client1)와 프로젝트(some-cool-project-name)가 있습니다.

다른 부모(클라이언트) 아래에 새 게시물을 만들고 게시물에 동일한 이름(및 슬래그)을 지정하면 워드프레스가 슬래그에 -2를 추가합니다. "/work/client2/some-cool-project-name-2"

사용자 지정 게시 유형:

// Custom taxonomies.
function custom_taxonomies() {
    $args = array(
        'label' => __( 'Work', '' ),
        'labels' => array(
            'name' => __( 'Work', '' ),
            'singular_name' => __( 'Work', '' ),
        ),
        'description' => '',
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_rest' => false,
        'rest_base' => '',
        'has_archive' => true,
        'show_in_menu' => true,
        'exclude_from_search' => false,
        'capability_type' => 'post',
        'map_meta_cap' => true,
        'hierarchical' => true,
        'rewrite' => array( 'slug' => 'work/%client_name%', 'with_front' => true ),
        'query_var' => true,
        'menu_icon' => 'dashicons-hammer',
        'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'page-attributes' ),
        'taxonomies' => array( 'client_name' ),
    );
    register_post_type( 'work', $args );

    $args = array(
        'label' => __( 'Clients', '' ),
        'labels' => array(
            'name' => __( 'Clients', '' ),
            'singular_name' => __( 'Client', '' ),
        ),
        'public' => true,
        'hierarchical' => false,
        'label' => 'Clients',
        'show_ui' => true,
        'show_in_menu' => true,
        'show_in_nav_menus' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'work/client_name', 'with_front' => false, ),
        'show_admin_column' => false,
        'show_in_rest' => false,
        'rest_base' => '',
        'show_in_quick_edit' => false,
    );
    register_taxonomy( 'client_name', array( 'work' ), $args );
}
add_action( 'init', 'custom_taxonomies' );

그리고 내 파마링크 다시 쓰기:

// Replace URL with proper taxonomy structure.
function permalink_rewrites( $link, $post ) {
    if ( $post->post_status !== 'publish' || $post->post_type != 'work' ) {
        return $link;
    }

    if ( $post->post_type == 'work' ) {
        $type = '%client_name%/';
        $filters = get_the_terms( $post->ID, 'client_name' );
        $slug = $filters[0]->slug . '/';
    }

    if ( isset( $slug ) ) {
        $link = str_replace( $type, $slug, $link );
    }

    return $link;
}
add_filter( 'post_type_link', 'permalink_rewrites', 10, 2 );

이 문제를 해결할 수 있는 방법에 대한 제안이 있습니까?

감사해요.

안타깝게도 워드프레스는 실제로 이런 방식으로 설계되지 않았습니다.두 개의 게시물/CPT가 서로 다른 범주에 있더라도 이 기능이 작동하지 않는 이유 중 일부는 두 범주에 모두 포함될 때 발생하는 현상입니까?당신은 몇 가지 끔찍한 다시 쓰기 규칙과 관련된 기능을 얻기 시작해야 할 것입니다. 그 시점에서 당신은 단지 와주에서 404개의 오류를 요청하는 것입니다.

다행히도, 같은 민달팽이를 가진 분류학과 CPT에 의존하는 대신에, 여러분이 할 수 있는 일이 있습니다.대신 분류법 부분을 삭제하고 사용자 정의 게시 유형의 계층 형식을 사용할 수 있습니다.

이것이 작동하는 이유의 일부는 하나의 포스트/CPT에 여러 부모를 할당할 수 없기 때문에 구문별 충돌이 발생하지 않기 때문입니다.

다음과 같은 새 "작업"을 만듭니다.Client 1라고 불리는 두 번째.Client 2.

이제 이러한 "상위 작업"을 생성하여 다음과 같은 세 번째 "작업"을 생성할 수 있습니다.Cool Project상위 항목을 다음으로 설정합니다.Client 1그런 다음 네 번째 이름을 만듭니다.Cool Project또한 부모님을 설정합니다.Client 2.

이렇게 하면 다음과 같은 퍼멀링크 구조를 얻을 수 있습니다.

https://example.com/work/client-1/cool-project
https://example.com/work/client-2/cool-project

지금 바로 여기에서 이 기능을 확인할 수 있습니다.

이것들은 제가 언급한 방식과 정확히 일치합니다.

이것의 단점은, 만약 당신이 그것을 사용한다면./work/client-name페이지를 표시하려면 보관 템플릿의 기능을 대신 사용하려면 포스트 유형 템플릿(WP 4.7.0 이후 사용 가능)을 설정해야 합니다.

그러나 리디렉션 및 규칙 다시 쓰기가 필요하지 않습니다.

다음은 퍼멀링크 구조의 스크린샷입니다.

다음은 CPT 개요 관리 페이지의 스크린샷입니다.

언급URL : https://stackoverflow.com/questions/50846733/custom-post-type-slug-clash

반응형