Make WordPress Core

Opened 11 years ago

Closed 11 years ago

#33860 closed enhancement (wontfix)

Loop helpers function

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

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 11 years ago.
33860.diff​ (3.4 KB ) - added by swissspidy 11 years ago.
33860.2.diff​ (3.9 KB ) - added by swissspidy 11 years ago.

Download all attachments as: .zip

Change History (13)

#1 @SergeyBiryukov
11 years ago

  • Component General → Query
  • Focuses template added

#2 @johnbillion
11 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
11 years ago

#3 @swissspidy
11 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
11 years ago

#4 @swissspidy
11 years ago

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

#5 @obenland
11 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
11 years ago

  • Keywords close added

#7 @swissspidy
11 years ago

  • Milestone Awaiting Review
  • Resolution → wontfix
  • Status new → 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
11 years ago

  • Keywords close removed
  • Resolution wontfix
  • Status closed → reopened

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

#9 @SergeyBiryukov
11 years ago

  • Milestone → Awaiting Review

#10 @obenland
11 years ago

  • Keywords 2nd-opinion good-first-bug removed
  • Milestone Awaiting Review
  • Resolution → wontfix
  • Status reopened → closed
  • Version 4.3

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.