Skip to content

Instantly share code, notes, and snippets.

@frontend-coder
Last active July 10, 2022 17:58
Show Gist options
  • Save frontend-coder/6c54e32b8f70b20cfa86e2d35e2fe1a9 to your computer and use it in GitHub Desktop.
Save frontend-coder/6c54e32b8f70b20cfa86e2d35e2fe1a9 to your computer and use it in GitHub Desktop.
19. Створення власної таксономії до custom post type #wordpress
function wpdocs_codex_book_init() {
$labels = array(
'name' => esc_html_x( 'Books', 'Post type general name', 'textdomain' ),
'singular_name' => esc_html_x( 'Book', 'Post type singular name', 'textdomain' ),
'menu_name' => esc_html_x( 'Books', 'Admin Menu text', 'textdomain' ),
'name_admin_bar' => esc_html_x( 'Book', 'Add New on Toolbar', 'textdomain' ),
'add_new' => esc_html__( 'Add New', 'textdomain' ),
'add_new_item' => esc_html__( 'Add New Book', 'textdomain' ),
'new_item' => esc_html__( 'New Book', 'textdomain' ),
'edit_item' => esc_html__( 'Edit Book', 'textdomain' ),
'view_item' => esc_html__( 'View Book', 'textdomain' ),
'all_items' => esc_html__( 'All Books', 'textdomain' ),
'search_items' => esc_html__( 'Search Books', 'textdomain' ),
'parent_item_colon' => esc_html__( 'Parent Books:', 'textdomain' ),
'not_found' => esc_html__( 'No books found.', 'textdomain' ),
'not_found_in_trash' => esc_html__( 'No books found in Trash.', 'textdomain' ),
'featured_image' => esc_html_x( 'Book Cover Image', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'textdomain' ),
'set_featured_image' => esc_html_x( 'Set cover image', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
'remove_featured_image' => esc_html_x( 'Remove cover image', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
'use_featured_image' => esc_html_x( 'Use as cover image', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
'archives' => esc_html_x( 'Book archives', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'textdomain' ),
'insert_into_item' => esc_html_x( 'Insert into book', 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'textdomain' ),
'uploaded_to_this_item' => esc_html_x( 'Uploaded to this book', 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'textdomain' ),
'filter_items_list' => esc_html_x( 'Filter books list', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'textdomain' ),
'items_list_navigation' => esc_html_x( 'Books list navigation', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'textdomain' ),
'items_list' => esc_html_x( 'Books list', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'textdomain' ),
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'book' ),
'capability_type' => 'post',
'has_archive' => false, // for test
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
);
register_post_type( 'book', $args );
// потрібно створити файл taxonomy.php taxonomy-brand.php
if( !taxonomy_axists('brand') ) {
// зарегіструвати таксономію для кастомного типу запису book та other-post-type-name
register_taxonomy( 'brand', array('book', 'other-post-type-name' ), $aee );
}
$aee = array(
'hierarchical' => false,
'show_ui'=> true,
'query_var' => true,
'rewrite' => array( 'slug' => 'brands' ),
'show_admin_column' => true,
'show_in_rest' => true,
'labels' => array(
'name' => esc_html_x( 'Brands', 'taxonomy general name', 'textdomain' ),
'singular_name' => esc_html_x( 'Brand', 'taxonomy singular name', 'textdomain' ),
'search_items' => esc_html__( 'Search Brands', 'textdomain' ),
'all_items' => esc_html__( 'All Brands', 'textdomain' ),
'parent_item' => esc_html__( 'Parent Brand', 'textdomain' ),
'parent_item_colon' => esc_html__( 'Parent Brand:', 'textdomain' ),
'edit_item' => esc_html__( 'Edit Brand', 'textdomain' ),
'update_item' => esc_html__( 'Update Brand', 'textdomain' ),
'add_new_item' => esc_html__( 'Add New Brand', 'textdomain' ),
'new_item_name' => esc_html__( 'New Brands Name', 'textdomain' ),
'menu_name' => esc_html__( 'Brands', 'textdomain' ),
)
)
}
add_action( 'init', 'wpdocs_codex_book_init' );
<?php
get_header()
$term = get_term_by( 'slug', get_query_var('term'), get_query_var('taxonomy') );
echo $term ->name;
?>
<div>
<?php if( have_posts() ) : while( have_posts() ) : the_post(); ?>
<?php get_template_part('tamplate-part/content', 'car'); ?>
<?php endwhile; else : ?>
<?php get_template_part('tamplate-part/content', 'none'); ?> // постів не знайдено
<?php endif; ?>
</div>
<?php get_footer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment