Проблема с постоянными ссылками и пользовательской таксономией / пользовательским типом записи - PullRequest
0 голосов
/ 03 февраля 2020

У меня есть пользовательский тип сообщения и пользовательская таксономия. Моя желаемая структура постоянных ссылок:

Страница основного архива: / custom-post-type (или / how-to /) Страница архива таксономии: / custom-post-type / taxonomy (или / how-to / taxonomy ) Страница отдельного поста: / custom-post-type / таксономия / 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 );

1 Ответ

1 голос
/ 03 февраля 2020

Пожалуйста, обновите параметр has_archive в пределах register_post_type аргументов. От 'has_archive' => true, до 'has_archive' => 'how-to', и обновите настройки постоянной ссылки, чтобы восстановить правила перезаписи и проверить, надеюсь, это поможет вам!

...