To move the current coupon to the top of the loop, you can use an approach that involves storing the current coupon data temporarily and then outputting it separately before looping through the rest of the coupons.
Replace /* Your condition to determine current coupon */
with the condition that identifies the current coupon within your loop
<?php
$current_coupon = null; // Initialize variable to store current coupon
// First, find and store the current coupon
while ( have_posts() ) {
the_post();
$post_id = get_the_ID();
wpcoupon_setup_coupon( get_post( $post_id ) );
// Check if this is the current coupon
if ( /* Your condition to determine current coupon */ ) {
$current_coupon = [
'post_id' => $post_id,
// Store any other relevant data about the current coupon
];
continue; // Skip outputting the current coupon for now
}
// Output other coupons
get_template_part( 'loop/loop-coupon', $loop_tpl );
}
// Output the current coupon separately at the top
if ($current_coupon) {
wpcoupon_setup_coupon( get_post( $current_coupon['post_id'] ) );
get_template_part( 'loop/loop-coupon', $loop_tpl );
}
?>