Make WordPress Core

Opened 10 years ago

Closed 10 years ago

#33860 closed enhancement (wontfix)

Loop helpers function

Reported by: sebastianpisula's profile sebastian.pisula Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Query Keywords: has-patch has-unit-tests
Focuses: template Cc:

Description

while(have_post()) {
the_post();

if(is_loop_first()){
echo 'first';
}

if(is_loop_index(5)){
echo 'is 5 index element';
}

if(is_loop_last()){
echo 'last';
}

}

Attachments (3)

loop.patch (1.7 KB) - added by sebastian.pisula 10 years ago.
33860.diff (3.4 KB) - added by swissspidy 10 years ago.
33860.2.diff (3.9 KB) - added by swissspidy 10 years ago.

Download all attachments as: .zip

Change History (13)

#1 @SergeyBiryukov
10 years ago

  • Component changed from General to Query
  • Focuses template added

#2 @johnbillion
10 years ago

  • Keywords has-patch needs-unit-tests 2nd-opinion good-first-bug added

In the past I've had the need to determine first/last/odd/even posts in the loop.

I think these functions need better names, and we'll need to decide whether first/last means the first/last in the current loop or the first/last in the current loop's query. Might be worth taking a look at some theme frameworks to see if there's prior art.

These will of course need unit tests.

@swissspidy
10 years ago

#3 @swissspidy
10 years ago

  • Keywords has-unit-tests added; needs-unit-tests removed

33860.diff adds three helper functions: query_post_index(), query_is_first_post() and query_is_last_post(). Including tests.

Getting the first and last post is usually needed more often, as odd/even queries are usually used for styling only (which you can target with CSS). If you want to detect the odd/even post, you can create your own helper function leveraging query_post_index().

@swissspidy
10 years ago

#4 @swissspidy
10 years ago

33860.2.diff is an updated patch after the recent changes to the factory properties.

#5 @obenland
10 years ago

I'm not convinced we really need these. It doesn't feel like we're gaining a lot by them for the cost of having to maintain them going forward.

#6 @swissspidy
10 years ago

  • Keywords close added

#7 @swissspidy
10 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

I'm not convinced we really need these. It doesn't feel like we're gaining a lot by them for the cost of having to maintain them going forward.

It's easy to implement this on your own if you wish, but I think for now this is not something we should do.

#8 @sebastian.pisula
10 years ago

  • Keywords close removed
  • Resolution wontfix deleted
  • Status changed from closed to reopened

I think that this is good idea to add this to core. This is interesting helper

#9 @SergeyBiryukov
10 years ago

  • Milestone set to Awaiting Review

#10 @obenland
10 years ago

  • Keywords 2nd-opinion good-first-bug removed
  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from reopened to closed
  • Version 4.3 deleted

I feel like it being a good idea, or interesting, is still not a good enough reason to add these.

The original proposition hasn't changed since this ticket was closed. There should be a Core need to adopt these functions, which is currently not there.

Note: See TracTickets for help on using tickets.