Community Forums › Forums › Archived Forums › General Discussion › CPT archive with Multiple Loops
Tagged: genesis_loop, posts_per_page, WP_query
- This topic has 19 replies, 3 voices, and was last updated 4 years, 10 months ago by
LanceHillier.
-
AuthorPosts
-
June 24, 2020 at 11:43 pm #499540
LanceHillier
ParticipantHi Everyone,
I have been having a hard time figuring this out, I know this code is wrong but I'm having trouble getting it to be where I need it to be.
What I'm trying to build is an archive-videos.php file that has two loops for
beginner-videos
and 'performance-videos'.At the moment it's doing what it's supposed to except, it's not limiting the number of post by 3 for each loop.
I know the queries are wrong and the first loop is missing the:
if($myloop2->have_posts()) : while($myloop2->have_posts()) : $myloop2->the_post();
but I haven't been able to figure out how... Here is the code...
<?php //* Force full-width-content layout setting add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' ); add_action( 'genesis_loop', 'lh_ukevideos_loop' ); remove_action( 'genesis_loop', 'genesis_do_loop' ); /** * Uke Charts Loop * */ function lh_ukevideos_loop() { if (have_posts()) : while (have_posts()) : the_post(); //WP loop ?> <?php the_content(); ?> <?php $args=array( //Loop 1 'post_type' => 'videos', 'taxonomy' => 'video-tags', 'term' => 'beginner-videos', 'posts_per_page' => 3 ); $myloop = new WP_Query($args); $videos = get_post_meta( get_the_ID(), 'beginner_videos', true ); if( $videos ) { for( $i = 0; $i < $videos; $i++ ) { $title = esc_html( get_post_meta( get_the_ID(), 'beginner_videos_' . $i . '_title', true ) ); $video = esc_url( get_post_meta( get_the_ID(), 'beginner_videos_' . $i . '_video', true ) ); $thumbnail = (int) get_post_meta( get_the_ID(), 'beginner_videos_' . $i . '_thumbnail', true ); // Thumbnail field returns image ID, so grab image. If none provided, use default image $thumbnail = $thumbnail ? wp_get_attachment_image( $thumbnail, 'be_video' ) : '<img src="' . get_stylesheet_directory_uri() . '/images/default-video.png" />'; // Displayed in three columns, so using column classes $class = 0 == $i || 0 == $i % 3 ? 'one-third first' : 'one-third'; // Build the video box echo '<div class="' . $class . '"><a href="' . $video . '">' . $thumbnail . '</a>' . $title . '</div>'; } } endwhile; endif; ?> <?php wp_reset_query(); // end beginner videos loop ?> <?php $args2=array( //Loop 2 'post_type' => 'videos', 'taxonomy' => 'video-tags', 'term' => 'performance-videos', 'posts_per_page' => 3 ); $myloop2 = new WP_Query($args2); if($myloop2->have_posts()) : while($myloop2->have_posts()) : $myloop2->the_post(); $videos = get_post_meta( get_the_ID(), 'performance_videos', true ); if( $videos ) { for( $i = 0; $i < $videos; $i++ ) { $p_title = esc_html( get_post_meta( get_the_ID(), 'performance_videos_' . $i . '_title', true ) ); $p_video = esc_url( get_post_meta( get_the_ID(), 'performance_videos_' . $i . '_video', true ) ); $p_thumbnail = (int) get_post_meta( get_the_ID(), 'performance_videos_' . $i . '_thumbnail', true ); // Thumbnail field returns image ID, so grab image. If none provided, use default image $p_thumbnail = $p_thumbnail ? wp_get_attachment_image( $p_thumbnail, 'be_video' ) : '<img src="' . get_stylesheet_directory_uri() . '/images/default-video.png" />'; // Displayed in three columns, so using column classes $classs = 0 == $i || 0 == $i % 3 ? 'one-third first' : 'one-third'; // Build the video box echo '<div class="' . $classs . '"><a href="' . $p_video . '">' . $p_thumbnail . '</a>' . $p_title . '</div>'; } } ?> <?php endwhile; endif; ?> <?php wp_reset_query(); // end beginner videos loop ?> <?php } ?> <?php genesis();
If anyone has a moment to run an eye over, and any guidance would be really appreciated.
Thank you!!
p.s. the site is only local at the moment
http://www.notlive.comJune 24, 2020 at 11:50 pm #499541Brad Dalton
ParticipantTry a different argument https://developer.wordpress.org/reference/classes/wp_query/#pagination-parameters
I think there maybe something like showposts or something like that as well you could try or maybe posts_per_archive_page.
The genesis featured posts widget would have the correct parameter to use.
June 24, 2020 at 11:57 pm #499542LanceHillier
ParticipantThanks Brad, I'll look through that, just to clarify the two loops are coming from CPT's and a repeater field in each set up with a title, video and thumbnail.
It's been about a week of getting this far, would be great to know if I'm on the right track with it all?
June 25, 2020 at 12:10 am #499543Brad Dalton
ParticipantI understand the time involved as i have written hundreds of loops like this.
I didn't test your code but it looks like you only need to get the argument right to limit the entries per loop on the archive page. It would be like the featured posts widget as there's no pagination.
June 25, 2020 at 12:18 am #499544Brad Dalton
ParticipantJune 25, 2020 at 12:26 am #499545LanceHillier
ParticipantYeah thanks Brad, I love it but I am way over my head here I think.
One of the problems I'm having with the arguments is that if I put the `if($myloop->have_posts()) : while($myloop->have_posts()) :
$myloop->the_post();`back on the first loops $arg, it runs the code twice. I have it set up like two loops inside a main loop and I wonder if that's wrong.
I'm looking for answers for the arguments in the ACF documentation right now as, are these fields even classified as posts?
June 25, 2020 at 12:52 am #499546Brad Dalton
ParticipantCustom fields store data added posts. You can call this data in your loop.
Your issue relates to how many posts to show in each loop which is controlled by different parameters as previously mentioned.
The section of code you need to modify is the $args array.
$args = array( 'posts_per_page' => 3 );
posts_per_page is the parameter. 3 is the value and these equal 1 argument.
June 25, 2020 at 4:27 am #499547andytc
Participantshouldn't it be -
'posts_per_page' => '3'
maybe missing ' ' around the 3 ?
June 26, 2020 at 10:48 pm #499592Brad Dalton
ParticipantAlso note, you don't need to code 2 loops for this. You can use one which displays entries for both taxonomy terms as they use the same CPT and same taxonomy, just different term.
June 27, 2020 at 8:18 pm #499620LanceHillier
ParticipantHi Brad,
Is it still possible to have different number of posts in a single loop?
Do you now how I can get the two loops in containers so I can put a titles and style? they seem to be in the Video Box and in columns but I don't seem to be able to style them as a whole
Thanks again mate!!
June 28, 2020 at 2:26 am #499625Brad Dalton
ParticipantAnything is possible with code and a few hours work.
Inspect the element, find the HTML tags wrapping the container and add CSS.
June 28, 2020 at 6:19 pm #499691LanceHillier
ParticipantHi Brad,
Sorry for taking up your time mate.
The problem I'm having with the styling is that their doesn't appear to be a HTML tag wrapping the containers for the loop. I can just put in a div around the loops but when I inspect element on the first loop for example the div is in the left hand corner but doesn't contain the videos in the loop.
Does that make sense?
I know this code...
// Build the video box echo '<div class="' . $classs . '"><a href="' . $p_video . '">' . $p_thumbnail . '</a>' . $p_title . '</div>'; }
...Is building the Video Box and it's putting them all in a column class of three which has been defined with the $class variable.
Just not sure how to contain those video boxes in a wrapping element that actually contains them.
June 28, 2020 at 6:51 pm #499694LanceHillier
ParticipantOk, the wraps are just basic html structure that for some reason my brain didn't connect the dots...
June 28, 2020 at 7:19 pm #499696Brad Dalton
ParticipantJune 28, 2020 at 8:21 pm #499698LanceHillier
ParticipantEverything is laid out how I want it too now, Just need to figure out how to call the taxonomy/term title for the two loops and get the $args working to limit posts.
I've just sat down now to spend the day on it
June 28, 2020 at 8:27 pm #499699LanceHillier
ParticipantWhen I use
get_the_title()
it echo's out the second loops titleJune 28, 2020 at 8:30 pm #499700LanceHillier
ParticipantHere is my current code:
<?php //* Force full-width-content layout setting add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' ); add_action( 'genesis_loop', 'lh_ukevideos_loop' ); remove_action( 'genesis_loop', 'genesis_do_loop' ); /** * Uke Charts Loop * */ function lh_ukevideos_loop() {?> <?php $args=array( //Loop 1 'post_type' => 'videos', 'taxonomy' => 'video-tags', 'term' => 'beginner-videos', 'posts_per_page' => '3' ); $myloop = new WP_Query($args);?> <div class="beginner-videos"> <div class="wrap"> <?php if ( $myloop->have_posts() ): $count = 2; while ( $myloop->have_posts() ): $myloop->the_post(); $videos = get_post_meta( get_the_ID(), 'beginner_videos', true ); if( $videos ) { for( $i = 0; $i < $videos; $i++ ) { $title = esc_html( get_post_meta( get_the_ID(), 'beginner_videos_' . $i . '_title', true ) ); $video = esc_url( get_post_meta( get_the_ID(), 'beginner_videos_' . $i . '_video', true ) ); $thumbnail = (int) get_post_meta( get_the_ID(), 'beginner_videos_' . $i . '_thumbnail', true ); // Thumbnail field returns image ID, so grab image. If none provided, use default image $thumbnail = $thumbnail ? wp_get_attachment_image( $thumbnail, 'be_video' ) : '<img src="' . get_stylesheet_directory_uri() . '/images/default-video.png" />'; // Displayed in three columns, so using column classes $class = 0 == $i || 0 == $i % 3 ? 'one-third first' : 'one-third'; 'clearfix'; // Build the video box echo '<div class="' . $class . '"><a href="' . $video . '">' . $thumbnail . '</a>' . $title . '</div>'; } } endwhile; endif; ?> </div> </div> <?php wp_reset_query(); // end beginner videos loop ?> <?php $args2=array( //Loop 2 'post_type' => 'videos', 'taxonomy' => 'video-tags', 'term' => 'event-videos', 'posts_per_page' => '3' ); $myloop2 = new WP_Query($args2); if($myloop2->have_posts()) : while($myloop2->have_posts()) : $myloop2->the_post();?> <div class="event-videos"> <div class="wrap"> <?php $videos = get_post_meta( get_the_ID(), 'event_videos', true ); if( $videos ) { for( $i = 0; $i < $videos; $i++ ) { $title = esc_html( get_post_meta( get_the_ID(), 'event_videos_' . $i . '_title', true ) ); $video = esc_url( get_post_meta( get_the_ID(), 'event_videos_' . $i . '_video', true ) ); $thumbnail = (int) get_post_meta( get_the_ID(), 'event_videos_' . $i . '_thumbnail', true ); // Thumbnail field returns image ID, so grab image. If none provided, use default image $thumbnail = $thumbnail ? wp_get_attachment_image( $thumbnail, 'be_video' ) : '<img src="' . get_stylesheet_directory_uri() . '/images/default-video.png" />'; // Displayed in three columns, so using column classes $classs = 0 == $i || 0 == $i % 3 ? 'one-third first' : 'one-third'; 'clearfix'; // Build the video box echo '<div class="' . $classs . '"><a href="' . $video . '">' . $thumbnail . '</a>' . $title . '</div>'; } } ?> <?php endwhile; endif; ?> <?php wp_reset_query(); // end beginner videos loop ?> </div> </div> <?php } ?> <?php genesis();
June 28, 2020 at 10:41 pm #499701LanceHillier
Participantok, figured everything out except getting the $args to limit the number of posts in each loop.
Could WordPress core be interfering with the $args somehow?
June 28, 2020 at 10:52 pm #499702LanceHillier
ParticipantAnd would love to know how to do this in a single loop, do you have an example I could look at?
July 1, 2020 at 9:55 pm #499824LanceHillier
ParticipantHi Brad, Just as a follow-up, I managed to get this working and turns out I was looking in completely the wrong place, the 'Videos' were coming from an ACF repeater field so wp_query wasn't working. I started then looking at how to limit ACF fields and priced together this:
if( $videos ) {
for( $i = 0; $i < $videos; $i++ ) {
if($i==6){ break; }Which now limits the videos to 6.
Happy days!! Thanks again for your help!!
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.