• Hey, I’m working on a new theme, it has two columns.

    In column 1 i want to list 5 even posts, in column two i’d like to have 5 odd posts.

    Does anyone know how I could do this?

    Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • the-worlds-greatest-blogger

    (@the-worlds-greatest-blogger)

    My best guess would be define even as a multiple of two… Then on the odd post if even = false then show the post.

    Thread Starter threevisual

    (@threevisual)

    thanks for the comment, sadly I have no idea how to put that into code (I can just about code wordpress templates but not php).

    at the moment i’m using this for the first column:

    <?php global $post; $myposts = get_posts('numberposts=5'); foreach($myposts as $post) : setup_postdata($post);?>

    I just need to adapt it so it shows even / odd posts.

    Thread Starter threevisual

    (@threevisual)

    anyone have an idea of how I could get this to work?

    how do you define an odd post?

    this might work (untested):

    <?php
    global $post; $myposts = get_posts('numberposts=10');
    ?>
    <div class="column-odd">
    <?php
    $i=1;
    foreach($myposts as $post) :
    if($i%2 != 0) :
    setup_postdata($post);
    //output odd post in the usual way
    endif;
    $i++;
    endforeach;
    ?>
    </div>
    <div class="column-even">
    <?php
    $i=1;
    foreach($myposts as $post) :
    if($i%2 == 0) :
    setup_postdata($post);
    //output even post in the usual way
    endif;
    $i++;
    endforeach;
    ?>
    </div>

    styles:

    .column-odd{ float:left; width: 48%; }
    .column-even { float:left; width: 48%; }

    Thread Starter threevisual

    (@threevisual)

    Worked like a charm. Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Get odd / even posts’ is closed to new replies.