Sanjeev Kumar
Sanjeev Kumar

Reputation: 3163

Showing last entry as first PHP

I am trying to display last entry as first and tried to use it as array but not able to get it, here is the code I have:

<?php
$maxItem = 30;
for ($i=1; $i<$maxItem + 1; $i++) {
    if (have_rows('content_block_'.$i)) {
        while (have_rows('content_block_'.$i)) {
        the_row();
?>

Current Output

<div class="content_block_1">1</div>
<div class="content_block_2">2</div>
<div class="content_block_3">3</div>
<div class="content_block_4">4</div>

Expected Output

<!-- if new entry -->
<div class="content_block_5">5</div>
<div class="content_block_1">1</div>
<div class="content_block_2">2</div>
<div class="content_block_3">3</div>
<div class="content_block_4">4</div>

Upvotes: 0

Views: 51

Answers (1)

nice_dev
nice_dev

Reputation: 17805

<?php

$maxItem = 30;
for ($i = 0; $i < $maxItem; $i++) {
    $block_item = $i == 0 ? $maxItem : $i;
    while (have_rows('content_block_'.$block_item)) {
        the_row();
    }
}

You can simply check if value of $i is 0. If yes, go for the last entry, i.e, $maxItem, otherwise, complete the rest of it sequentially.

Upvotes: 1

Related Questions