admin管理员组

文章数量:1125586

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

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 );
}
?>
Share Improve this question edited Mar 7, 2024 at 14:40 Tony Djukic 2,2594 gold badges18 silver badges33 bronze badges asked Mar 6, 2024 at 23:52 user3418508user3418508 133 bronze badges 3
  • 2 What makes a coupon the 'current' coupon? – Jacob Peattie Commented Mar 7, 2024 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 Commented Mar 7, 2024 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 – user3418508 Commented Mar 9, 2024 at 0:19
Add a comment  | 

1 Answer 1

Reset to default 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 );
}
?>

本文标签: phpWordPress move current to top in the loop