Skip to content

Instantly share code, notes, and snippets.

@michelve
Last active June 18, 2018 14:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michelve/720ec81a32767f7b498fcec8513b9862 to your computer and use it in GitHub Desktop.
Save michelve/720ec81a32767f7b498fcec8513b9862 to your computer and use it in GitHub Desktop.
create odd and even layout for wp post loop
<?php
$args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'author_name' => '',
'post_status' => 'publish',
'suppress_filters' => true,
'fields' => '',
);
//Set up a counter
$counter = 0;
$query = new WP_Query( $args );
if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post(); $counter++;
// We are in loop so we can check if counter is odd or even
// Change 2 to 3 to do it every 3 posts
if( $counter % 2 == 0 ) : ?>
<?php the_title(); //Echo the title of post ?>
<?php the_content(); //Echo the content of the post ?>
<?php else: ?>
<!-- even or odd results -->
<?php the_title(); //Echo the title of post ?>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
@michelve
Copy link
Author

Adds a custom layout every 2 or 3 post inside a loop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment