Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other how to display first and second post differently than other posts

  • This topic is empty.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #35068
    ahmed18
    Participant

    how to display first and second post differently than other posts in wordpress

    #90258
    DanaB
    Member

    I would probably suggest something like…

    .article:nth-of-type(1), .article:nth-of-type(2) {
    //code
    }

    where you replace article with whatever class name your posts have.

    #90262
    ahmed18
    Participant

    thx alot for helping me
    but this method if i want to change simple things like colors and bg
    i want to change everything i want method to change through ” if ” in php files and i don’t know it :(

    #90263
    WCMiller
    Member

    you could declare an iterator variable (say $iter) right before you start the loop, and iterate it after each pass, then use it to test if it’s the first or second pass through the loop Here’s a pseudo-code example:

    $iter = 0;
    if (have_posts()) :
    while (have_posts()) :
    switch($iter){
    case 0:
    /* Code for first post */
    case 1:
    /* Code for second post */
    default:
    /* Code for other posts */
    }
    $iter++;
    endwhile;
    endif;

    Something like that (optimized for what you’re doing) should probably work for you.

    #90265
    ahmed18
    Participant

    do u mean this is true ?


    if (have_posts()) :
    $counter = 1;
    while (have_posts()) :
    the_post();
    if( $counter == 1 ) { ?>

    /*here the code to show this first post different*/



    /*here the code to show this second post different*/



    /*here the code to show all other posts*/

    $counter++;
    endwhile;
    endif;
    ?>
    #90268
    WCMiller
    Member

    Pretty much. However, if you’re going to use if statements, you’ll need to make the “if” before ($counter == 2) into an “else if”, otherwise you’ll have the first post show up twice: once as a “first post”, and once as an “all other posts”. That’s one of the reasons why I went with a switch statement.

    One thing you should also be aware of is the fact that if you have pagination, you’ll probably want to include a check for whether the page being displayed is the first page. I don’t know the code off the top of my head, but it shouldn’t be too hard to check for.

    #90418
    ahmed18
    Participant

    thx alot for helping
    and this is correct loop if anyone need use it

    
    if (have_posts()) :
    $counter = 1;
    while (have_posts()) :
    the_post();
    if( $counter == 1 ) { ?>

    /*here the code to show this first post different*/



    /*here the code to show this second post different*/



    /*here the code to show all other posts*/

    $counter++;
    endwhile;
    endif;
    ?>

    can u plz ur code using cases but as before , i mean leave only places for post classes and put php codes

Viewing 7 posts - 1 through 7 (of 7 total)
  • The forum ‘Other’ is closed to new topics and replies.