1

I have a custom post type - let's assume it's called fruit

If I then have a list of fruits, ordered by date:

Apple
Orange
Mango
Pineapple
Orange

If I load the Mango page for example via single.php I want to know whether it's an odd or an even post so I can use some slightly different CSS classes in the template.

I could get all posts of type fruit in date order and cycle through them to find this out, but this seems incredibly wasteful.

Is there a better way?

MrCarrot
  • 2,546
  • 1
  • 23
  • 29
  • you will have to use a custom query to get the sql line as extra parameter and then just check in php if `$line % 2`, trying to find references on mysql extra line parameter – Nikos M. Apr 16 '15 at 17:42
  • 1
    How about checking the id of the post. If its odd or even? Also why do you need to get that info? What are you trying to do? – Aryeh Armon Apr 16 '15 at 17:43
  • I did think about the ID of the post, but that's no guarantee because a post could be deleted, marked as draft or the date changed (and therefore the order changed). I want posts to be styled slightly differently depending on whether they're odd or even (at the moment I have a link that loads the next post page in the sequence). The posts are ordered by date so an odd one is post number 1, even post number 2, and so on. – MrCarrot Apr 16 '15 at 17:54
  • Is this a 'display issue' of alternate posts (lines) being displayed in a different format on the screen? – Ryan Vincent Apr 16 '15 at 17:56
  • Yes, I want to echo some different css classes in the template for alternate posts – MrCarrot Apr 16 '15 at 17:57
  • It is about display only - basically odd posts I want text aligned left and even posts I want the text aligned right. Most things that talk about this on the web are referring to a list of posts "the loop" rather than a single page outside of "the loop" – MrCarrot Apr 16 '15 at 20:18
  • you can also get the mysql count of all previous posts (alternative to computing `row number`), see [my answer](http://stackoverflow.com/a/29682290/3591273) – Nikos M. Apr 17 '15 at 04:47
  • You can pass a parameter in the URL and use that as a method to determine odd or even – Pieter Goosen Apr 17 '15 at 05:17

3 Answers3

0

How about using array_search("Mango",$array)%2 ?

http://php.net/manual/en/function.array-search.php

vlex
  • 131
  • 12
  • I'd still need to get every post though into $array. There could be hundreds or thousands of posts so ideally I wanted a more efficient way – MrCarrot Apr 16 '15 at 17:41
  • 1
    Well you need to post a bit of context source, otherwise we're just guessing here. Also you'd need to include the search context (in what context is the result an even or odd-numbered result) in order to be able to find that out, so I doubt there's going to be any more efficient way of determining that.. – vlex Apr 16 '15 at 18:40
  • The order is in order of date. So the oldest post would be odd, the next oldest post even, and so on up until the newest post. Or the other way around (it doesn't really matter) – MrCarrot Apr 16 '15 at 20:20
  • No I meant how your data is structured. Without context we can't propose any feasible solutions really. Also are you going through all post, or a single one, or just a selected few of 'em? Additionally I'd have to note out, that if this is so important about the way your code works, maybe it's worth investing some effort in coding this into the DB model, or whatever the source of data you're working with is, so you can simply and without any performance and resource penalties call the argument for each post.. – vlex Apr 17 '15 at 07:33
0

You will have to use a custom query to get the sql row number as extra parameter and then just check in php if $row_num % 2

see here on how to get the row number in a mysql select statement

You can do a custom sql query in php (and bypass wordpress) or even use wordpress query to alter the default WP query to get the posts

Note that this method assumes you will select at least 2 posts and is efficient (as mysql handles the selecting) and will work regardles if post ids are indeed chronologicaly ordered or not or in any other ordering needed. For a single post, probably two select statements (or a combined select statement) will be needed (see below for an alternative).

An alternative is to select the count of all posts which come before the needed post (with given ordering), this would also give you the row number (or row number +1) and then proceed as previously. This will probably need two select statements (i.e one for the post and one for the count of all previous posts up to the given post acording to given ordering, e.g by date)

Community
  • 1
  • 1
Nikos M.
  • 8,033
  • 4
  • 36
  • 43
0

If you never delete a post, you can save that with post title, something like O_Mango or E_Pineapple when you create a post ('O_' for odd, 'E_' for even). Then in single.php

if(substr($post_title, 0, 2) === 'O_') {
  echo 'This is an odd post';
}
else {
  echo 'This is an even post';
}
echo substr($post_title, 2); //The title to be used on the page
Sam
  • 93
  • 1
  • 9