1

how could i get for every post on a page a post_class "first"?

with this code a managed a class "first_post" for the really first post of the loop, but i need it also on the second, third and so on page of the loop.

function.php

function firstpost_class($class) {
    global $post, $posts;
    if ( is_home() && !is_paged() && ($post == $posts[0]) ) $class[] = 'firstpost';
    return $class;
}
add_filter('post_class', 'firstpost_class');

Thanks for your help. Google couldn't help yet.

4

1 回答 1

-1

I've seen this kind of thing done on the loop itself -- comment to clarify if you want to do this site-wide, or particularly want to hook into post_class (then again, you can edit loop.php or a content-LOOPTYPE.php and reuse it over and over). Here's an example loop:

<?php
$counter = 0;
if ( have_posts() ) : while ( have_posts() ) : the_post();
$counter++;
?>

<div class="post <?php echo 'item-' . $counter; ?>">
  <?php the_title(); ?>
  <?php the_content(); ?>
</div>

<?php endwhile; endif; ?>

This would give you item-1, item-2, etc. etc.

于 2012-06-22T17:11:47.990 回答