Displaying different content for odd even pages

Hi,

I’m trying to find out about a php script for displaying different content based on the odd or even numbers of a post. I have an author box set up with information about me but I would like to create another version of this automatically depending on if the post is even or odd.

If even display this version
If odd display this version etc…

Does this have a particular name or if anyone has a link to a guide that would be amazing!

Thanks

if ( $post&1 )
{
	displayOddVersion();
}
else
{
	displayEvenVersion();
}

Divide by 2 is fine, but if in the future you want to divide the post number by 3 or 4, or later 5 etc then you would do well to familiarise yourself with the modulus math operator (%).


$posts = 1233;

if(($posts % 2) === 1){  // use a strict check here
echo "do the odd thing";
}else{
echo "do the even thing";
}

Then you can take that same idea and extract that leftover amount, and program your script to handle the remainder as say, a function or a class.

This is far more extensible, and less prone to error than writing out a long if/else clause. Just imagine you decided to provide 20 different scenarios?


$posts = 1233;

$target = 3 ;

$leftover = $posts % $target;

function thing3(){
echo 'do the 3 leftover thing';
}

function thing2(){
echo 'do the 2 leftover thing';
}

function thing1(){
echo 'do the 1 leftover thing';
}

function thing0(){
echo 'do the 0 leftover thing';
}

$op = "thing". $leftover;

$op();