Advertisement
Guest User

Untitled

a guest
Oct 21st, 2021
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.28 KB | None | 0 0
  1. add_action('init', function () {
  2.     // список параметров: https://wp-kama.ru/function/register_taxonomy
  3.     register_taxonomy('product-category', ['products'], [
  4.         'label'                 => '',
  5.         'labels'                => [
  6.             'name'              => 'Categories',
  7.             'singular_name'     => 'Categories',
  8.             'search_items'      => 'Search Categories',
  9.             'all_items'         => 'All Categories',
  10.             'view_item '        => 'View Category',
  11.             'parent_item'       => 'Parent Category',
  12.             'parent_item_colon' => 'Parent Category:',
  13.             'edit_item'         => 'Edit Category',
  14.             'update_item'       => 'Update Category',
  15.             'add_new_item'      => 'Add New Category',
  16.             'new_item_name'     => 'New Category Name',
  17.             'menu_name'         => 'Categories',
  18.         ],
  19.         'description'           => '',
  20.         'public'                => true,
  21.         'hierarchical'          => true,
  22.         'rewrite'               => [
  23.             'slug'         => 'catalog',
  24.             'hierarchical' => true,
  25.             'with_front'   => false,
  26.         ],
  27.     ]);
  28.  
  29.     // https://wp-kama.ru/function/register_post_type
  30.     register_post_type('products', [
  31.         'label'  => null,
  32.         'labels' => [
  33.             'name'               => 'Products',
  34.             'singular_name'      => 'Product',
  35.             'add_new'            => 'Add new',
  36.             'add_new_item'       => 'Add new product',
  37.             'edit_item'          => 'Edit product',
  38.             'new_item'           => 'New product',
  39.             'view_item'          => 'View product',
  40.             'search_items'       => 'Find product',
  41.             'not_found'          => 'Products nof found',
  42.             'not_found_in_trash' => 'In basket product not found',
  43.             'parent_item_colon'  => '',
  44.             'menu_name'          => 'Products',
  45.         ],
  46.         'description'         => '',
  47.         'public'              => true,
  48.         'show_in_rest'        => false,
  49.         'rest_base'           => '',
  50.         'hierarchical'        => false,
  51.         'supports'            => ['title', 'editor', 'thumbnail'],
  52.         'taxonomies'          => ['product-category'],
  53.         'has_archive'         => 'catalog',
  54.         'rewrite'             => [
  55.             'slug'         => 'catalog/%product-category%',
  56.             'with_front'   => false,
  57.         ],
  58.         'query_var'           => true,
  59.     ]);
  60. });
  61. function products_permalink($permalink, $post)
  62. {
  63.     if (strpos($permalink, '%product-category%') === FALSE) return $permalink;
  64.  
  65.     $terms = get_the_terms($post, 'product-category');
  66.     if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) {
  67.         $taxonomy_slug =  get_term_parents_list($terms[0]->term_id, 'product-category', [
  68.             'separator' => '/',
  69.             'format'    => 'slug',
  70.             'link'      => false,
  71.             'inclusive' => true,
  72.         ]);
  73.         $taxonomy_slug = mb_substr($taxonomy_slug, 0, -1);
  74.     } else
  75.         $taxonomy_slug = 'no-products';
  76.  
  77.     var_dump($taxonomy_slug);
  78.  
  79.     return str_replace('%product-category%', $taxonomy_slug, $permalink);
  80. }
  81. add_filter('post_type_link', 'products_permalink', 10, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement