Skip to content

Instantly share code, notes, and snippets.

@kachi
Created March 10, 2012 17:50
Show Gist options
  • Select an option

  • Save kachi/2012281 to your computer and use it in GitHub Desktop.

Select an option

Save kachi/2012281 to your computer and use it in GitHub Desktop.
<?php
function additional_post_classes( $classes ) {
global $wp_query;
if( $wp_query->found_posts < 1 ) {
return $classes;
}
if( $wp_query->current_post == 0 ) {
$classes[] = 'post-first';//ループ内の最初の投稿にpost-firstというclass名を与える
}
if( $wp_query->current_post % 2 ) {
$classes[] = 'post-even';//ループ内の偶数番目の投稿にpost-evenというclass名を与える
} else {
$classes[] = 'post-odd';//ループ内の奇数番目の投稿にpost-evenというclass名を与える
}
if( $wp_query->current_post == ( $wp_query->post_count - 1 ) ) {
$classes[] = 'post-last';//ループ内の最後の投稿にpost-evenというclass名を与える
}
return $classes;
}
add_filter( 'post_class', 'additional_post_classes' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment