• Resolved EntarteteMuzak

    (@entartetemuzak)


    I want to display my posts in a grid and be able to style post 1,2,3,4 differently.

    <?php if (have_posts()) : ?>
    <?php $count = 0; ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php $count++; ?>
    <?php if ($count%2== 0) : ?>
    
    //Second post
    
    <?php else : ?>
    
    //First post
    
    <?php endif; ?>
    <?php endwhile; ?>
    <?php endif; ?>

    I tried this, but with no luck;

    <?php if (have_posts()) : ?>
    <?php $count = 0; ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php $count++; ?>
    <?php if ($count%1== 0) : ?>
    
    //First post
    
    <?php elseif ($count%2== 0) : ?>
    
    //Second post
    
    <?php elseif ($count%3== 0) : ?>
    
    //Third post
    
    <?php else : ?>
    
    //Fourth post
    
    <?php endif; ?>
    <?php endwhile; ?>
    <?php endif; ?>

Viewing 6 replies - 1 through 6 (of 6 total)
  • <?php $count++; ?>
    <?php if ($count%4 == 1 ) : ?>
    
    //First post
    
    <?php elseif ($count%4 == 2 ) : ?>
    
    //Second post
    
    <?php elseif ($count%4 == 3 ) : ?>
    
    //Third post

    $count%4 is the remainder of dividing the count by 4.

    if you are interested, search for ‘php modulus operator’

    Thread Starter EntarteteMuzak

    (@entartetemuzak)

    Thank you!
    I’ll try and look into the modulus operator…

    What would I have to change to pull in either 3 posts and style each one differently, or pull in say 5 posts and just style the first and last?

    Any help would be appreciated.

    Forgot to click notify button before.

    for three posts – each position totally different:

    <?php $count = 0; ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php $count++; ?>
    <?php if ($count%3== 1) : ?>
    //First post
    <?php elseif ($count%3== 2) : ?>
    //Second post
    <?php else : ?>
    //Third post
    <?php endif; ?>
    <?php endwhile; ?>

    for 5 posts, first and last different from the others:

    <?php $count = 0; ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php $count++; ?>
    <?php if ($count%5== 1) : ?>
    //First post
    <?php elseif ($count%5== 0) : ?>
    //Last post
    <?php else : ?>
    //nr 2,3,4 post
    <?php endif; ?>
    <?php endwhile; ?>

    the above is really only if you want the html structure and output to be different; for simply different css styling, try to integrate the position into the post_class() , for instance;
    example for creating cyclic css classes

    This is somewhat what I need but not exactly.

    How about if the number of post is dependent in the number indicated in wp dashboard > settings > reading?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Post grid that adds code to post 1,2,3,4’ is closed to new replies.