簡體   English   中英

自定義帖子類型Slug沖突

[英]Custom Post Type Slug clash

我有多個帶有自定義分類的自定義帖子類型。 盡管有不同的父母,我仍然有一次slu cla沖突。

這是我的URL結構:/ work /%client_name%/%project_name%

我有一個客戶端(client1)和項目(some-cool-project-name)生成這個slug:“/ work / client1 / some-cool-project-name”。

當我在不同的父(客戶端)下創建一個新帖子並給帖子提供相同的名稱(和slug)時,wordpress將-2添加到slug:“/ 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 );

關於我能做什么的任何建議都可以解決這個問題?

謝謝。

不幸的是,WordPress並沒有真正以這種方式設計。 為什么這對於2個帖子/ CPT甚至在不同的類別中都不起作用的部分原因是當兩個類別中都有這種情況時會發生什么 你必須開始得到一些討厭的重寫規則和涉及的redirect_canonical()函數 - 此時你只是要求 wazoo中的404錯誤。

幸運的是,您可以做一些事情,而不是依賴於具有相同slug的分類法和CPT。 您可以改為刪除它的分類法部分並使用自定義帖子類型的分層格式。

這可行的部分原因是因為您無法將多個父項分配給一個帖子/ CPT,因此沒有permastruct沖突。

創建一個名為Client 1的新“Work”,另一個名為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頁面顯示任何內容,您現在必須設置一個Post Type模板 (自WP 4.7.0起可用)以實現您擁有的功能而是使用存檔模板。

但是,它可以防止重定向和重寫規則的需要。

這是永久鏈接結構的屏幕截圖: 這是永久鏈接結構的屏幕截圖

以下是CPT概述管理頁面的屏幕截圖: 以下是CPT概述管理頁面的屏幕截圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM