同类相关产品related products的写法

<?php // Get the current product ID
$current_product_id = get_the_ID();

// Get the series terms for the current product
$series_terms = wp_get_post_terms($current_product_id, 'series', array('fields' => 'ids'));

if (!empty($series_terms) && !is_wp_error($series_terms)) {
    
    // Query for related products in the same series
    $args = array(
        'post_type'      => 'product',
        'posts_per_page' => 8,
        'post__not_in'   => array($current_product_id), // Exclude current product
        'orderby'        => 'rand', // Random order, change to 'date' or 'title' if needed
        'post_status'    => 'publish',
        'tax_query'      => array(
            array(
                'taxonomy' => 'series',
                'field'    => 'term_id',
                'terms'    => $series_terms,
            ),
        ),
    );
    
    $related_products = new WP_Query($args);
    
    // Only display if we have related products
    if ($related_products->have_posts()) : ?>
    
        <div class="ab-template-3">
            <h5 class="ab-border-orange ab-orange">Related products</h5>
        </div>
        
        <div class="ab-template-40">
            <div class="ab-flex-jc-sb ab-position-title ab-scale ab-text-align-c">
                
                <?php while ($related_products->have_posts()) : $related_products->the_post(); 
                    $product_title = get_the_title();
                    $product_link = get_permalink();
                    
                    // Get featured image
                    if (has_post_thumbnail()) {
                        $product_image = get_the_post_thumbnail_url(get_the_ID(), 'medium');
                    } else {
                        // Fallback placeholder image
                        $product_image = get_template_directory_uri() . '/images/placeholder.jpg';
                    }
                ?>
                
                <div class="ab-flex-item ab-flex-item4">
                    <div class="ab-item-img">
                        <a href="<?php echo esc_url($product_link); ?>">
                            <img 
                                alt="<?php echo esc_attr($product_title); ?>" 
                                title="<?php echo esc_attr($product_title); ?>" 
                                width="220" 
                                height="220" 
                                src="<?php echo esc_url($product_image); ?>"
                                loading="lazy"
                            >
                        </a>
                    </div>
                    <p class="ab-item-title">
                        <a href="<?php echo esc_url($product_link); ?>">
                            <?php echo esc_html($product_title); ?>
                        </a>
                    </p>
                </div>
                
                <?php endwhile; ?>
                
            </div>
        </div>
        
        <?php
        // Reset post data
        wp_reset_postdata();
        
    endif; // End if have_posts
    
} // End if series terms exist
?>
×
同类相关产品related products的写法

同类相关产品related products的写法

<?php // Get the ...