Forums

[resolved] Post grid that adds code to post 1,2,3,4 (7 posts)

  1. EntarteteMuzak
    Member
    Posted 1 year ago #

    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; ?>
  2. alchymyth
    The Sweeper
    Posted 1 year ago #

    <?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'

  3. EntarteteMuzak
    Member
    Posted 1 year ago #

    Thank you!
    I'll try and look into the modulus operator...

  4. flint_and_tinder
    Member
    Posted 10 months ago #

    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.

  5. flint_and_tinder
    Member
    Posted 10 months ago #

    Forgot to click notify button before.

  6. alchymyth
    The Sweeper
    Posted 10 months ago #

    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

  7. streetrose
    Member
    Posted 3 months ago #

    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?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags