星期四 , 23 1 月 2025

怎么在woo产品单页上显示自定义分类类型

注意这个implode的用法,很有效:


function custom_product_model_and_serial_number() {
    global $product;

    // Get the terms for the 'model' custom taxonomy
    $terms = get_the_terms( $product->get_id(), 'model' );

    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
        $model_list = array();

        foreach ( $terms as $term ) {
            // Create a clickable link for each term
            $model_list[] = '<a href="' . esc_url( get_term_link( $term ) ) . '" title="' . esc_attr( $term->name ) . '">' . esc_html( $term->name ) . '</a>';
        }

        // Display the model terms
        echo '<p><strong>' . esc_html__( 'Model:', 'your-text-domain' ) . '</strong> ' . implode( ', ', $model_list ) . '</p>';
    }

    // Get the Serial Number from the custom field
    $serial_number = get_post_meta( $product->get_id(), '_serial_number', true );

    if ( ! empty( $serial_number ) ) {
        // Display the Serial Number
        echo '<p><strong>' . esc_html__( 'Serial Number:', 'your-text-domain' ) . '</strong> ' . esc_html( $serial_number ) . '</p>';
    }
}
add_action( 'woocommerce_single_product_summary', 'custom_product_model_and_serial_number', 40 );