Last active
September 9, 2021 17:28
-
-
Save rachelmccollin/93772b5f9a86936aebe4d03b530c3d89 to your computer and use it in GitHub Desktop.
WPMU DEV One Query Multiple Loops
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 while ( $query->have_posts() ) : $query->the_post(); | |
if ( get_post_type( $post->ID ) == 'book' && $count == 0 ) { | |
include( locate_template( 'template-parts/loop-frontpage.php' )); | |
$count++; | |
} | |
endwhile; // end first loop for books | |
rewind_posts(); |
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
while ( $query->have_posts() ) : $query->the_post(); // start fourth loop for book club | |
if ( get_post_type( $post->ID ) == 'page' && $post->post_name == 'bookclub' && $count == 3 ) { | |
include(locate_template( 'template-parts/loop-frontpage.php' )); | |
$count++; | |
} | |
endwhile; // end fourth loop for page | |
rewind_posts(); | |
?> |
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
include(locate_template( 'template-parts/loop-frontpage.php' )); |
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
$count = 0; | |
?> | |
<section class="blog-posts grid"> | |
</section> |
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
<article id="post-<?php the_ID(); ?>" <?php post_class( 'quarter left' ); ?>> |
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
while ( $query->have_posts() ) : $query->the_post(); // start second loop for wordpress posts | |
if ( get_post_type( $post->ID ) == 'post' && in_category( 'wordpress' ) && $count == 1 ) { | |
include(locate_template( 'template-parts/loop-frontpage.php' )); | |
$count++; | |
} | |
endwhile; // end second loop for wordpress | |
rewind_posts(); |
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
.quarter { | |
width: 23.5%; | |
margin: 0 0.5% | |
} | |
.left { | |
float: left; | |
} |
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
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> | |
<?php if ( $count == 0 ) { | |
$title = 'Books'; | |
} | |
elseif ( $count == 1 ) { | |
$title = 'WordPress'; | |
} | |
elseif ( $count == 2 ) { | |
$title = 'News'; | |
} | |
elseif ( $count == 3 ) { | |
$title = 'Book Club'; | |
} | |
?> | |
<h2><?php echo $title;?></h2> | |
<?php if ( has_post_thumbnail() ) { ?> | |
<a href="<?php the_permalink(); ?>"> | |
<?php | |
the_post_thumbnail( 'grid' ); | |
?> | |
</a> | |
<?php } ?> | |
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> | |
<?php the_excerpt(); ?> | |
</article> |
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
while ( $query->have_posts() ) : $query->the_post(); // start third loop for news posts | |
if ( get_post_type( $post->ID ) == 'post' && in_category( 'news' ) && !in_category( 'wordpress' ) && $count == 2 ) { | |
include(locate_template( 'template-parts/loop-frontpage.php' )); | |
$count++; | |
} | |
endwhile; // end third loop for news | |
rewind_posts(); |
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
$args = array( | |
'post_type' => array ( 'book', 'post', 'page' ) | |
); | |
$query = new WP_query ( $args ); | |
if ( $query->have_posts() ) { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you put this altogether in order? I tried following the post – https://wpmudev.com/blog/how-to-use-one-query-to-run-multiple-loops/
However, my loop still seems to be stuck.