Last active
June 18, 2018 14:22
-
-
Save michelve/720ec81a32767f7b498fcec8513b9862 to your computer and use it in GitHub Desktop.
create odd and even layout for wp post loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adds a custom layout every 2 or 3 post inside a loop