Lego2012
12/13/2016 - 3:43 PM

Conditionally break posts

Conditionally break posts

<?php
//* Do NOT include the opening php tag shown above. Copy the code shown below.

// Conditionally break posts (from 2nd onwards) into 2 columns with featured post on first page
add_filter( 'post_class', 'sk_post_class' );
function sk_post_class( $classes ) {

    global $wp_query;
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

    // Check if we are on first page of pagination
    if ( 1 == $paged )  {

        if ( 0 == $wp_query->current_post ) {
            // Add 'featured-post' class to first post
            $classes[] = 'featured-post';
        } else {
            // Add 'one-half' class to all posts besides first
            $classes[] = 'one-half';
        }
        // Skip the first post and add 'first' class to every other
        if ( 1 == $wp_query->current_post % 2 ) {
            $classes[] = 'first';
        }

    } else {
        // On all other pages add 'one-half' to all posts
        $classes[] = 'one-half';
    }
    return $classes;

}