So weisen Sie einem Produkt eine einzelne Produktvorlage anhand seiner Kategorie zu (WordPress/WooCommerce/Block-Theme)Php

PHP-Programmierer chatten hier
Anonymous
 So weisen Sie einem Produkt eine einzelne Produktvorlage anhand seiner Kategorie zu (WordPress/WooCommerce/Block-Theme)

Post by Anonymous »

Ich habe versucht, dem Produkt eine einzelne Produktvorlage anhand seiner Kategorie zuzuordnen. Ich hatte nach ähnlichen Fragen gesucht, aber Antworten im klassischen Thema gefunden. Unten sind zwei Beispiele für mehrere Codes, die Gemini mir gegeben hat, von denen keiner funktioniert hat -> jedes Produkt ist auf die Standardvorlage statt auf bestimmte Vorlagen zurückgegriffen.
Code 1

Code: Select all

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 );
Code 2

Code: Select all

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 );

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post