Я попытался назначить продукту один шаблон по его категории. Я искал похожие вопросы, но нашел ответы в классической теме. Ниже приведены два примера нескольких кодов, которые мне предоставили Gemini, и ни один из них не сработал -> каждый продукт вернулся к шаблону по умолчанию вместо назначенных шаблонов.
Код 1
function custom_product_template_by_category_fse( $block_template, $id, $template_type ) {
if ( $id !== 'single-product' || $template_type !== 'single' ) {
return $block_template;
}
$category_template_map = array(
// Product Category Slug => FSE Template Slug
'electronics' => 'single-product-tech',
'apparel' => 'single-product-fashion',
'books' => 'single-product-media',
);
if ( is_singular( 'product' ) ) {
$product_id = get_the_ID();
$terms = get_the_terms( $product_id, 'product_cat' );
if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
foreach ( $terms as $term ) {
$category_slug = $term->slug;
if ( array_key_exists( $category_slug, $category_template_map ) ) {
$template_slug_to_use = $category_template_map[$category_slug];
$theme_slug = get_stylesheet();
$new_template = get_block_template( "{$theme_slug}//{$template_slug_to_use}", 'single' );
if ( $new_template ) {
return $new_template;
}
}
}
}
}
return $block_template;
}
add_filter( 'get_block_template', 'custom_product_template_by_category_fse', 10, 3 );
Код 2
function custom_block_template_include_by_category_final_25( $template ) {
if ( is_singular( 'product' ) ) {
$product_id = get_the_ID();
$terms = get_the_terms( $product_id, 'product_cat' );
$theme_dir = get_stylesheet_directory();
$template_origin_slug = 'twentytwentyfour';
$category_template_map = array(
'electronics' => 'single-product-tech',
'apparel' => 'single-product-fashion',
'books' => 'single-product-media',
);
if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
foreach ( $terms as $term ) {
$category_slug = $term->slug;
if ( array_key_exists( $category_slug, $category_template_map ) ) {
$template_slug_to_use = $category_template_map[ $category_slug ];
$wrapper_template_path = $theme_dir . '/templates/single-product.html';
if ( file_exists( $wrapper_template_path ) ) {
add_filter( 'block_template_meta', function( $meta, $slug ) use ( $template_slug_to_use, $template_origin_slug ) {
if ( $slug === 'single-product' ) {
$meta['slug'] = $template_slug_to_use;
$meta['theme'] = $template_origin_slug;
}
return $meta;
}, 99, 2 );
return $wrapper_template_path;
}
}
}
}
}
return $template;
}
add_filter( 'template_include', 'custom_block_template_include_by_category_final_25', 100 );
Подробнее здесь: https://stackoverflow.com/questions/797 ... dpress-woo
Как назначить один шаблон продукта продукту по его категории (тема WordPress/WooCommerce/Блок) ⇐ Php
Кемеровские программисты php общаются здесь
1761818382
Anonymous
Я попытался назначить продукту один шаблон по его категории. Я искал похожие вопросы, но нашел ответы в классической теме. Ниже приведены два примера нескольких кодов, которые мне предоставили Gemini, и ни один из них не сработал -> каждый продукт вернулся к шаблону по умолчанию вместо назначенных шаблонов.
Код 1
function custom_product_template_by_category_fse( $block_template, $id, $template_type ) {
if ( $id !== 'single-product' || $template_type !== 'single' ) {
return $block_template;
}
$category_template_map = array(
// Product Category Slug => FSE Template Slug
'electronics' => 'single-product-tech',
'apparel' => 'single-product-fashion',
'books' => 'single-product-media',
);
if ( is_singular( 'product' ) ) {
$product_id = get_the_ID();
$terms = get_the_terms( $product_id, 'product_cat' );
if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
foreach ( $terms as $term ) {
$category_slug = $term->slug;
if ( array_key_exists( $category_slug, $category_template_map ) ) {
$template_slug_to_use = $category_template_map[$category_slug];
$theme_slug = get_stylesheet();
$new_template = get_block_template( "{$theme_slug}//{$template_slug_to_use}", 'single' );
if ( $new_template ) {
return $new_template;
}
}
}
}
}
return $block_template;
}
add_filter( 'get_block_template', 'custom_product_template_by_category_fse', 10, 3 );
Код 2
function custom_block_template_include_by_category_final_25( $template ) {
if ( is_singular( 'product' ) ) {
$product_id = get_the_ID();
$terms = get_the_terms( $product_id, 'product_cat' );
$theme_dir = get_stylesheet_directory();
$template_origin_slug = 'twentytwentyfour';
$category_template_map = array(
'electronics' => 'single-product-tech',
'apparel' => 'single-product-fashion',
'books' => 'single-product-media',
);
if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
foreach ( $terms as $term ) {
$category_slug = $term->slug;
if ( array_key_exists( $category_slug, $category_template_map ) ) {
$template_slug_to_use = $category_template_map[ $category_slug ];
$wrapper_template_path = $theme_dir . '/templates/single-product.html';
if ( file_exists( $wrapper_template_path ) ) {
add_filter( 'block_template_meta', function( $meta, $slug ) use ( $template_slug_to_use, $template_origin_slug ) {
if ( $slug === 'single-product' ) {
$meta['slug'] = $template_slug_to_use;
$meta['theme'] = $template_origin_slug;
}
return $meta;
}, 99, 2 );
return $wrapper_template_path;
}
}
}
}
}
return $template;
}
add_filter( 'template_include', 'custom_block_template_include_by_category_final_25', 100 );
Подробнее здесь: [url]https://stackoverflow.com/questions/79795207/how-to-assign-single-product-template-to-product-by-its-category-wordpress-woo[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия