• I recently found a post on here that showed me how to display recent posts and excerpts on a static home page. It works great and my code is displayed below.

    What I’m wondering is how to add a class to the divs that contain each of the two most recent posts that get displayed so that there’s some margin between the two posts.

    These divs only exist dynamically when I use get_post to call information from my blog.

    Hopefully this makes sense. I’m not good at php and just don’t know what to do. Thanks.

    <?php
     $postslist = get_posts('numberposts=2&order=ASC&orderby=title');
     foreach ($postslist as $post) :
        setup_postdata($post);
     ?>
     <div>
     <span style="text-decoration:underline; color:#ffff99; font-weight:bold"><?php the_date(); ?></span>
     <br />
     <b><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </b>
     <?php the_excerpt(); ?>
     <a href="<?php the_permalink(); ?>">Continue Reading...</a>
    
     </div>
     <?php endforeach; ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • Try:

    <?php
     $postslist = get_posts('numberposts=2&order=ASC&orderby=title');
     $c = 0 // set up a post counter
     $class = '';
     foreach ($postslist as $post) :
        setup_postdata($post);
        $c++;
        if( $c <3 ) $class = ' class="toptwo"';
     ?>
     <div <?php echo $class;?>>
     <span style="text-decoration:underline; color:#ffff99; font-weight:bold"><?php the_date(); ?></span>
     <br />
     <b><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </b>
     <?php the_excerpt(); ?>
     <a href="<?php the_permalink(); ?>">Continue Reading...</a>
    
     </div>
     <?php endforeach; ?>
    Thread Starter nimaha

    (@nimaha)

    Thanks for your response. I got an error that says unexpected T_VARIABLE on line 17 which would be this:

    $class = ”;

    Ack! missed a ; in the line above. Updated code:

    <?php
     $postslist = get_posts('numberposts=2&order=ASC&orderby=title');
     $c = 0; // set up a post counter
     $class = '';
     foreach ($postslist as $post) :
        setup_postdata($post);
        $c++;
        if( $c <3 ) $class = ' class="toptwo"';
     ?>
     <div <?php echo $class;?>>
     <span style="text-decoration:underline; color:#ffff99; font-weight:bold"><?php the_date(); ?></span>
     <br />
     <b><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </b>
     <?php the_excerpt(); ?>
     <a href="<?php the_permalink(); ?>">Continue Reading...</a>
    
     </div>
     <?php endforeach; ?>
    Thread Starter nimaha

    (@nimaha)

    Hence, the reason I limit my coding and stick more to designing. Too many ways to drive myself crazy.

    Worked perfectly the second go ’round. Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Adding classes to divs that are created dynamically through get_posts’ is closed to new replies.