Памятка для меня: как добавить кастомный тип поста в WordPress
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
/* Books */ function post_type_books() { $labels = array( 'name' => _x('Books', 'post type general name'), 'singular_name' => _x('Book', 'post type singular name'), 'add_new' => _x('Add New Book', 'book'), 'add_new_item' => __('Add New Book'), 'edit_item' => __('Edit Book'), 'new_item' => __('New Book'), 'view_item' => __('View Book'), 'search_items' => __('Search Book'), 'not_found' => __('No Books found'), 'not_found_in_trash' => __('No Books found in Trash'), 'parent_item_colon' => '' ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'query_var' => true, 'capability_type' => 'post', 'hierarchical' => false, 'menu_position' => null, 'supports' => array('title', 'editor','thumbnail','custom-fields'), 'rewrite' => array( "with_front" => false, "slug" => "books"), 'menu_icon' => 'dashicons-book' ); register_post_type( 'books', $args ); } add_action('init', 'post_type_books'); |
А тепрь добавляем таксономию WordPress для нового кастомного типа поста:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
function custom_taxonomy_authors() { $labels = array( 'name' => 'Authors', 'singular_name' => 'Author', 'menu_name' => 'Authors', 'all_items' => 'All Authors', 'parent_item' => 'Parent Authors', 'parent_item_colon' => 'Parent Authors:', 'new_item_name' => 'New Author', 'add_new_item' => 'Add New Author', 'edit_item' => 'Edit Author', 'update_item' => 'Update Author', 'separate_items_with_commas' => 'Separate authors with commas', 'search_items' => 'Search Authors', 'add_or_remove_items' => 'Add or remove author', 'choose_from_most_used' => 'Choose from the most used authors', 'not_found' => 'Not Found', ); $args = array( 'labels' => $labels, 'hierarchical' => false, 'public' => true, 'show_ui' => true, 'show_admin_column' => true, 'show_in_nav_menus' => false, 'show_tagcloud' => false, ); register_taxonomy('authors', array( 'books' ), $args); } add_action( 'init', 'custom_taxonomy_authors', 0 ); |
Располагаю добавление нового типа поста и таксономии обычно в файле function.php темы.
Тадам!