0

I have this code to loop coupon codes in store page.

Current coupons open in same page of store page.

I want move the current coupon to top of loop.

<?php
while ( have_posts() ) {
the_post();
wpcoupon_setup_coupon( get_post( get_the_ID() ) );
get_template_part( 'loop/loop-coupon', $loop_tpl );
}
?>
3
  • 2
    What makes a coupon the 'current' coupon? Mar 7 at 1:27
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Mar 7 at 14:40
  • this is code for id of the coupon, <?php echo wpcoupon_coupon()->ID; ?> I dont know the code for current coupon because the coupon open in same store. with pop up Mar 9 at 0:19

1 Answer 1

0

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 );
}
?>
1
  • this is code for id of the coupon, <?php echo wpcoupon_coupon()->ID; ?> I dont know the code for current coupon because the coupon open in same store. with pop up Mar 8 at 1:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.