Шаблон для пользовательской таксономии

Я потратил много времени, чтобы найти решение, но не повезло.

у меня есть собственный тип сообщения, называемый рецептом, который имеет типы рецептов (таксономии), например, A, B, C

Теперь я хочу перечислить все рецепты типа А на www.mydomain.com/recipes/A

мой пользовательский тип сообщения: recipe_cptи таксономия recipe_tx

я пытался taxonomy.php, taxonomy-recipe_tx.php но я получаю 404.

Может ли кто-нибудь помочь мне, как создать шаблон для пользовательской таксономии.

вот моя таможенная таксономия

function reg_recipe_taxtaxonomy() {

$labels = array(
    'name'                       => _x( 'Recipe Categories', 'Taxonomy General Name', 'text_domain' ),
    'singular_name'              => _x( 'Recipe Category', 'Taxonomy Singular Name', 'text_domain' ),
    'menu_name'                  => __( 'Recipe Types', 'text_domain' ),
    'all_items'                  => __( 'All Types', 'text_domain' ),
    'parent_item'                => __( 'Parent Type', 'text_domain' ),
    'parent_item_colon'          => __( 'Parent Type:', 'text_domain' ),
    'new_item_name'              => __( 'New Type Name', 'text_domain' ),
    'add_new_item'               => __( 'Add New Type', 'text_domain' ),
    'edit_item'                  => __( 'Edit Type', 'text_domain' ),
    'update_item'                => __( 'Update Type', 'text_domain' ),
    'separate_items_with_commas' => __( 'Separate Types with commas', 'text_domain' ),
    'search_items'               => __( 'Search Types', 'text_domain' ),
    'add_or_remove_items'        => __( 'Add or remove Types', 'text_domain' ),
    'choose_from_most_used'      => __( 'Choose from the most used Types', 'text_domain' ),
    'not_found'                  => __( 'Not Found', 'text_domain' ),
);
$rewrite = array(
        'slug'                       => 'recipes',
        'with_front'                 => true,
        'hierarchical'               => true,
    );
$args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
    'rewrite'                    => $rewrite,
);
register_taxonomy( 'recipe_tx', array( 'recipe_cpt' ), $args );

   }

      // Hook into the 'init' action
     add_action( 'init', 'reg_recipe_taxtaxonomy', 0 );

2 ответа

Решение

Я нашел решение. Создание шаблона для пользовательской таксономии.

если вы переписываете URL пользовательской таксономии

$rewrite = array(
        'slug'                       => 'recipes',
        'with_front'                 => true,
        'hierarchical'               => true,
    );

Затем вы должны удалить и воссоздать правила перезаписи. для этого я нашел функцию в Кодексе

просто позвони flush_rewrite_rules() после register_taxonomy()

окончательный код будет

function reg_recipe_taxtaxonomy() {

$labels = array(
    'name'                       => _x( 'Recipe Categories', 'Taxonomy General Name', 'text_domain' ),
    'singular_name'              => _x( 'Recipe Category', 'Taxonomy Singular Name', 'text_domain' ),
    'menu_name'                  => __( 'Recipe Types', 'text_domain' ),
    'all_items'                  => __( 'All Types', 'text_domain' ),
    'parent_item'                => __( 'Parent Type', 'text_domain' ),
    'parent_item_colon'          => __( 'Parent Type:', 'text_domain' ),
    'new_item_name'              => __( 'New Type Name', 'text_domain' ),
    'add_new_item'               => __( 'Add New Type', 'text_domain' ),
    'edit_item'                  => __( 'Edit Type', 'text_domain' ),
    'update_item'                => __( 'Update Type', 'text_domain' ),
    'separate_items_with_commas' => __( 'Separate Types with commas', 'text_domain' ),
    'search_items'               => __( 'Search Types', 'text_domain' ),
    'add_or_remove_items'        => __( 'Add or remove Types', 'text_domain' ),
    'choose_from_most_used'      => __( 'Choose from the most used Types', 'text_domain' ),
    'not_found'                  => __( 'Not Found', 'text_domain' ),
);
$rewrite = array(
        'slug'                       => 'recipes',
        'with_front'                 => true,
        'hierarchical'               => true,
    );
$args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
    'rewrite'                    => $rewrite,
);
register_taxonomy( 'recipe_tx', array( 'recipe_cpt' ), $args );
flush_rewrite_rules();
}

      // Hook into the 'init' action
     add_action( 'init', 'reg_recipe_taxtaxonomy', 0 );

надеюсь, это поможет кому-то!

Согласно шаблону WordPress Heirarchy вы можете создать файл, как это.

ваша таксономия равна = recer_tx ваша величина таксономии равна = A

Затем вы создаете два файла, как

taxonomy-a.php
taxonomy-a-recipe_tx.php

Это шаблон WordPress.

Я надеюсь, что это используется полностью для вас.

Другие вопросы по тегам