問題描述

我想建立一個與類別類似的自定義分類法,作為類別的行為與預設帖子 (基於/%category%/%postname%/permalink 結構),以便自定義帖子型別中的帖子是顯示為 www.example.com/custom-post-type/custom-taxonomy-name/post-name/post-name 另外,我希望類別元框僅在我們新增新的預設帖子時顯示,而不是在自定義帖子型別中新增新帖子,而自定義分類框僅顯示當我們在自定義帖子型別中新增新帖子時,而不是在新增新的預設帖子時。

最佳解決方案

首先,如果要僅顯示分類法,只能將自定義帖子型別顯示為自定義帖子型別,然後透過將自定義帖子型別名稱作為引數傳遞到 register_taxonomy,然後將分類法註冊到該自定義帖子型別。透過這樣做,分類法 metabox 只出現在自定義帖子型別上。如果您不想顯示類別 metabox 到自定義帖子型別,那麼在註冊自定義帖子型別時刪除術語類別作為引數,而是包含類似這樣的分類標題名稱’taxonomies’ => 陣列 (‘post_tag’,’your_taxonomy_name’) 。這裡是我如何實現的程式碼。我已經在自定義帖子型別主題下注冊了一個自定義分類法,並使用 slug themes_categories


function themes_taxonomy() {
    register_taxonomy(
        'themes_categories',  //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
        'themes',        //post type name
        array(
            'hierarchical' => true,
            'label' => 'Themes store',  //Display name
            'query_var' => true,
            'rewrite' => array(
                'slug' => 'themes', // This controls the base slug that will display before each term
                'with_front' => false // Don't display the category base before
            )
        )
    );
}
add_action( 'init', 'themes_taxonomy');

然後改變永久連結我建立了以下功能


function filter_post_type_link($link, $post)
{
    if ($post->post_type != 'themes')
        return $link;

    if ($cats = get_the_terms($post->ID, 'themes_categories'))
        $link = str_replace('%themes_categories%', array_pop($cats)->slug, $link);
    return $link;
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);

然後我註冊了一個自定義的帖子型別與 slug 主題如下


//Registering Custom Post Type Themes
add_action( 'init', 'register_themepost', 20 );
function register_themepost() {
    $labels = array(
        'name' => _x( 'Themes', 'my_custom_post','custom' ),
        'singular_name' => _x( 'Theme', 'my_custom_post', 'custom' ),
        'add_new' => _x( 'Add New', 'my_custom_post', 'custom' ),
        'add_new_item' => _x( 'Add New ThemePost', 'my_custom_post', 'custom' ),
        'edit_item' => _x( 'Edit ThemePost', 'my_custom_post', 'custom' ),
        'new_item' => _x( 'New ThemePost', 'my_custom_post', 'custom' ),
        'view_item' => _x( 'View ThemePost', 'my_custom_post', 'custom' ),
        'search_items' => _x( 'Search ThemePosts', 'my_custom_post', 'custom' ),
        'not_found' => _x( 'No ThemePosts found', 'my_custom_post', 'custom' ),
        'not_found_in_trash' => _x( 'No ThemePosts found in Trash', 'my_custom_post', 'custom' ),
        'parent_item_colon' => _x( 'Parent ThemePost:', 'my_custom_post', 'custom' ),
        'menu_name' => _x( 'Themes Posts', 'my_custom_post', 'custom' ),
    );

    $args = array(
        'labels' => $labels,
        'hierarchical' => false,
        'description' => 'Custom Theme Posts',
        'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'post-formats', 'custom-fields' ),
        'taxonomies' => array( 'post_tag','themes_categories'),
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'menu_icon' => get_stylesheet_directory_uri() . '/functions/panel/images/catchinternet-small.png',
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => array('slug' => 'themes/%themes_categories%','with_front' => FALSE),
        'public' => true,
        'has_archive' => 'themes',
        'capability_type' => 'post'
    );
    register_post_type( 'themes', $args );//max 20 charachter cannot contain capital letters and spaces
}  

註冊自定義帖子時,您需要記住的幾件事情。將 has_archive 引數更改為自定義帖子型別段號,另一個將重寫段名稱更改為’slug’ => 「custom_post_type_slug /%taxonomy_slug%

現在,當您在寫入帖子型別頁面中新增新的帖子型別時,您將看到 http://www.example.com/wordpress/themes/%themes_categories%/post-name/的固定連結。如果沒有選擇此帖子的自定義分類法,則固定連結將保留 http://www.example.com/wordpress/themes/%themes_categories%/post-name/,然後會顯示不良請求。為了糾正這個,我們在自定義分類法中建立一個預設的術語。 (與類別中未分類相同) 將其新增到 functions.php

function default_taxonomy_term( $post_id, $post ) {
    if ( 'publish' === $post->post_status ) {
        $defaults = array(
            'themes_categories' => array( 'other'),   //

            );
        $taxonomies = get_object_taxonomies( $post->post_type );
        foreach ( (array) $taxonomies as $taxonomy ) {
            $terms = wp_get_post_terms( $post_id, $taxonomy );
            if ( empty( $terms ) && array_key_exists( $taxonomy, $defaults ) ) {
                wp_set_object_terms( $post_id, $defaults[$taxonomy], $taxonomy );
            }
        }
    }
}
add_action( 'save_post', 'default_taxonomy_term', 100, 2 );

現在當自定義分類空白時,permlaink 自動成為 http://www.example.com/wordpress/themes/other/post-name/。

最後不要忘記透過點選管理部分中的永久連結設定儲存更改來重新整理重寫,否則將被重定向到 404 錯誤。希望這可以幫助你。

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。