永久链接和自定义 taxonomy/custom post 类型的问题

Problem with permalinks and custom taxonomy/custom post type

我有一个自定义 post 类型和一个自定义分类。我想要的永久链接结构是:

主要存档页面:/custom-post-type(或/how-to/) 分类归档页面:/custom-post-type/taxonomy(或/how-to/taxonomy) 单个 post 页:/custom-post-type/taxonomy/post(或 /how-to/taxonomy/post)

底部的 2 个正在运行,但主要存档页面指向“/how-to/%how-to_types%”并且不起作用。任何帮助将不胜感激。

// How-tos
// Register Custom Post Type How-to
function create_howto_cpt() {
    $labels = array(
        'name' => _x( 'How-tos', 'Post Type General Name', 'how-tos' ),
        'singular_name' => _x( 'How-to', 'Post Type Singular Name', 'how-tos' ),
        'menu_name' => _x( 'How-tos', 'Admin Menu text', 'how-tos' ),
        'name_admin_bar' => _x( 'How-to', 'Add New on Toolbar', 'how-tos' ),
        'archives' => __( 'How-to Archives', 'how-tos' ),
        'attributes' => __( 'How-to Attributes', 'how-tos' ),
        'parent_item_colon' => __( 'Parent How-to:', 'how-tos' ),
        'all_items' => __( 'All How-tos', 'how-tos' ),
        'add_new_item' => __( 'Add New How-to', 'how-tos' ),
        'add_new' => __( 'Add New', 'how-tos' ),
        'new_item' => __( 'New How-to', 'how-tos' ),
        'edit_item' => __( 'Edit How-to', 'how-tos' ),
        'update_item' => __( 'Update How-to', 'how-tos' ),
        'view_item' => __( 'View How-to', 'how-tos' ),
        'view_items' => __( 'View How-tos', 'how-tos' ),
        'search_items' => __( 'Search How-to', 'how-tos' ),
        'not_found' => __( 'Not found', 'how-tos' ),
        'not_found_in_trash' => __( 'Not found in Trash', 'how-tos' ),
        'featured_image' => __( 'Featured Image', 'how-tos' ),
        'set_featured_image' => __( 'Set featured image', 'how-tos' ),
        'remove_featured_image' => __( 'Remove featured image', 'how-tos' ),
        'use_featured_image' => __( 'Use as featured image', 'how-tos' ),
        'insert_into_item' => __( 'Insert into How-to', 'how-tos' ),
        'uploaded_to_this_item' => __( 'Uploaded to this How-to', 'how-tos' ),
        'items_list' => __( 'How-tos list', 'how-tos' ),
        'items_list_navigation' => __( 'How-tos list navigation', 'how-tos' ),
        'filter_items_list' => __( 'Filter How-tos list', 'how-tos' ),
    );
    $args = array(
        'label' => __( 'how-to', 'how-tos' ),
        'description' => __( 'How-to articles', 'how-tos' ),
        'labels' => $labels,
        'menu_icon' => 'dashicons-lightbulb',
        'supports' => array('title', 'author', 'editor', 'excerpt', 'thumbnail', 'custom-fields', 'revisions'),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'show_in_admin_bar' => true,
        'show_in_nav_menus' => true,
        'can_export' => true,
        'has_archive' => true,
        'hierarchical' => true,
        'exclude_from_search' => false,
        'show_in_rest' => true,
        'publicly_queryable' => true,
        'capability_type' => 'post',
        'rewrite' => array('slug' => 'how-to/%how-to_types%'),
    );
    register_post_type( 'how-to', $args );
}
add_action( 'init', 'create_howto_cpt', 0 );

// Register How-to Taxonomy
function create_howto_tax() {

  $labels = array(
    'name' => _x( 'Types', 'taxonomy general name' ),
    'singular_name' => _x( 'Type', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Types' ),
    'all_items' => __( 'All Types' ),
    'parent_item' => __( 'Parent Type' ),
    'parent_item_colon' => __( 'Parent Type:' ),
    'edit_item' => __( 'Edit Type' ),
    'update_item' => __( 'Update Type' ),
    'add_new_item' => __( 'Add New Type' ),
    'new_item_name' => __( 'New Type Name' ),
    'menu_name' => __( 'Types' ),
  );

  register_taxonomy('how-to_types',array('how-to'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_in_rest' => true,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array('slug' => 'how-to', 'with_front' => false),
  ));
}
add_action( 'init', 'create_howto_tax', 0 );

// Fixing permalinks
function how_to_permalinks( $post_link, $id = 0 ){
    $post = get_post($id);
        $terms = wp_get_object_terms( $post->ID, 'how-to_types' );
        if( $terms ){
            return str_replace( '%how-to_types%' , $terms[0]->slug , $post_link );
    } else {
        return str_replace( '%how-to_types%/' , '' , $post_link );
}

    return $post_link;
}
add_filter( 'post_type_link', 'how_to_permalinks', 1, 3 );

请更新 register_post_type 个参数中的 has_archive 个参数 从 'has_archive' => true,'has_archive' => 'how-to', 并更新您的永久链接设置以重新生成重写规则并检查,希望这对您有所帮助!