Forums

Get odd / even posts (6 posts)

  1. ThreeVisual
    Member
    Posted 2 years ago #

    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!

  2. The-Worlds-Greatest-Blogger
    Member
    Posted 2 years ago #

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

  3. ThreeVisual
    Member
    Posted 2 years ago #

    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.

  4. ThreeVisual
    Member
    Posted 2 years ago #

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

  5. alchymyth
    The Sweeper
    Posted 2 years ago #

    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%; }
  6. ThreeVisual
    Member
    Posted 2 years ago #

    Worked like a charm. Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic