• Resolved Clicknathan

    (@clicknathan)


    Would anyone know how to assign a different class to every other post in the loop?

    Similar to how you can add a class="alt" in the comments thread?

    I want to style every other post a little differently.

Viewing 6 replies - 1 through 6 (of 6 total)
  • <?php if (have_posts()) : ?>
             <?php
             $c = 0; // set up a counter so we know which post we're currently showing
             $extra_class = 'even' // set up a variable to hold an extra CSS class
             ?>
             <?php while (have_posts()) : the_post(); ?>
             <?php
             $c++; // increment the counter
             if( $c % 2 != 0) {
    	  // we're on an odd post
    	   $extra_class = 'odd';
             } else {
             $extra_class = 'even'; }
             ?>
    
             <div id="post-<?php the_ID(); ?>" <?php post_class($extra_class) ?>>     <!--Open the post DIV-->

    assigns a class of odd or even

    Very helpful, thanks! I appreciate the commenting too. ??

    whiteb0x

    (@whiteb0x)

    I’m sorry, but every time I’ve tried using this code it breaks my sidebar. ??

    Must be using it wrong…..

    this bit of code replaces your:

    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

    or similar…code

    I can’t be sure, but you must be using something that isn’t getting opened or closed….. every div that gets opened needs to be closed….

    I’ve tried this method and it doesn’t seem to be working for me it keeps outputting the same class on all the div’s

    the only thing I can think for it doing this is becuase I am already using the counter to display different styles for the first post, then for the next 7 and then for all the rest. but for the 7 (after the first post) I want to display alternative classes so they align perfectly on the screen.

    this is what I use now

    <?php  $c =0;
     if (have_posts()) : while (have_posts()) : the_post();
    $c++;?>
    
    <?php if( ($c == 1) && !is_paged(page) ) :?>
    
    //FEATURED POST MARKUP
    
    <?php elseif (($c = 7) && !is_paged(page))  :?>
    
    //HOME PAGE POST DISPLAY MARK UP, UP TO 7 POSTS ($c = 7) IS WHERE I PUT THE AMOUNT OF POSTS I WANTED TO DISPLAY ON THE HOME PAGE.
    
    /// BUT I want the 7 that display here to have alternative classes?
    
    <?php else :?>
    // MARK UP FOR ALL OTHER PAGES
    
    <?php endif;?>
    <?php endwhile; ?>

    any help would be much appreciated thanks in advanced

    /// BUT I want the 7 that display here to have alternative classes?

    what stops you to write different classes into the post div? or alternating classes?
    that is the purpose of the code, to give you total freedom on what to programm there.

    without seeing what you have tried so far, any suggestion is guesswork.

    if you paste the whole code of your template attempt into a https://wordpress.pastebin.com/ and post the link to it here, someone might be able to make more detailed suggestions.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘A Different Class for Every Other Post’ is closed to new replies.