R
R
Rasul Gitinov2018-04-13 14:07:32
WordPress
Rasul Gitinov, 2018-04-13 14:07:32

How to make a general file with the output of records and their categories?

Created a new post type "Works"

Reveal the code
// Works Post Type

add_action('init', 'register_post_type_works');

function register_post_type_works(){
  register_post_type('works', array(
    'label'  => null,
    'labels' => array(
      'name'               => 'Работы',
      'singular_name'      => 'Работа',
      'add_new'            => 'Добавить',
      'add_new_item'       => 'Добавление работы',
      'edit_item'          => 'Редактирование работы',
      'new_item'           => 'Новая работа',
      'view_item'          => 'Посмотреть работу',
      'search_items'       => 'Найти',
      'not_found'          => 'Не найдено',
      'not_found_in_trash' => 'Не найдено в корзине',
      'menu_name'          => 'Работы',
    ),
    'public'              => true,
    'show_in_menu'        => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 9,
    'menu_icon'           => 'dashicons-pressthis',
    'hierarchical'        => true,
    'supports'            => array('title', 'editor', 'thumbnail'),
    'taxonomies'          => array('works-cat'),
    'has_archive'         => true,
    'rewrite'             => true,
    'query_var'           => true
  ) );
}


and taxonomy "Categories" for them
Reveal the code
// Works Category

add_action('init', 'register_works_cat');

function register_works_cat(){
  register_taxonomy('works-cat', array('works'), array(
    'label'                 => '',
    'labels'                => array(
      'name'              => 'Категории',
      'singular_name'     => 'Категория',
      'all_items'         => 'Все',
      'view_item '        => 'Посмотреть категорию',
      'edit_item'         => 'Изменить категорию',
      'add_new_item'      => 'Добавить категорию',
      'new_item_name'     => 'Новая категория',
      'menu_name'         => 'Категории',
    ),
    'public'                => true,
    'hierarchical'          => true,
    'rewrite'               => true,
    'show_admin_column'     => false
  ) );
}


Then I created an archive page where all the works will be displayed ("archive-works.php"). Also on this page are displayed the categories of these records, which you can go through.
Opening a category opens a 404 page because there is no archive page for the "Categories" taxonomy.
The fact is that the page for displaying posts and the page for the category are the same page.
Is there any way to customize the taxonomies to display posts on the "archive-works.php" page instead of creating a "taxonomy-works-cat.php" page with the same code?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Vorotnev, 2018-04-13
@raselgit

1. Try to catch in the template_include filter by is_tax()
2. Or create all the same both templates in the hierarchy, and put the "repetitive code" in the template_part.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question