Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action('init', function () {
- // список параметров: https://wp-kama.ru/function/register_taxonomy
- register_taxonomy('product-category', ['products'], [
- 'label' => '',
- 'labels' => [
- 'name' => 'Categories',
- 'singular_name' => 'Categories',
- 'search_items' => 'Search Categories',
- 'all_items' => 'All Categories',
- 'view_item ' => 'View Category',
- 'parent_item' => 'Parent Category',
- 'parent_item_colon' => 'Parent Category:',
- 'edit_item' => 'Edit Category',
- 'update_item' => 'Update Category',
- 'add_new_item' => 'Add New Category',
- 'new_item_name' => 'New Category Name',
- 'menu_name' => 'Categories',
- ],
- 'description' => '',
- 'public' => true,
- 'hierarchical' => true,
- 'rewrite' => [
- 'slug' => 'catalog',
- 'hierarchical' => true,
- 'with_front' => false,
- ],
- ]);
- // https://wp-kama.ru/function/register_post_type
- register_post_type('products', [
- 'label' => null,
- 'labels' => [
- 'name' => 'Products',
- 'singular_name' => 'Product',
- 'add_new' => 'Add new',
- 'add_new_item' => 'Add new product',
- 'edit_item' => 'Edit product',
- 'new_item' => 'New product',
- 'view_item' => 'View product',
- 'search_items' => 'Find product',
- 'not_found' => 'Products nof found',
- 'not_found_in_trash' => 'In basket product not found',
- 'parent_item_colon' => '',
- 'menu_name' => 'Products',
- ],
- 'description' => '',
- 'public' => true,
- 'show_in_rest' => false,
- 'rest_base' => '',
- 'hierarchical' => false,
- 'supports' => ['title', 'editor', 'thumbnail'],
- 'taxonomies' => ['product-category'],
- 'has_archive' => 'catalog',
- 'rewrite' => [
- 'slug' => 'catalog/%product-category%',
- 'with_front' => false,
- ],
- 'query_var' => true,
- ]);
- });
- function products_permalink($permalink, $post)
- {
- if (strpos($permalink, '%product-category%') === FALSE) return $permalink;
- $terms = get_the_terms($post, 'product-category');
- if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) {
- $taxonomy_slug = get_term_parents_list($terms[0]->term_id, 'product-category', [
- 'separator' => '/',
- 'format' => 'slug',
- 'link' => false,
- 'inclusive' => true,
- ]);
- $taxonomy_slug = mb_substr($taxonomy_slug, 0, -1);
- } else
- $taxonomy_slug = 'no-products';
- var_dump($taxonomy_slug);
- return str_replace('%product-category%', $taxonomy_slug, $permalink);
- }
- add_filter('post_type_link', 'products_permalink', 10, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement