Problem with nested loops
-
Hi everyone !
I just met a big problem with nested loops.
I have a WP_Query (which is inside the main loop), and want to have in it two more loops by get_posts.
My second loop doesn’t work at all (videos).
My loop gets documents (custom post type), then inside it I get attachments and then only documents from a particular category (videos).
It’s been a long time I’m working on it… so maybe somebody could see the error, better than me !
Thanks a lot for your help.
Here’s the code :<?php $docs = new WP_Query('post_type=documents&orderby=date&order=ASC'); while ( $docs->have_posts() ) : $docs->the_post(); $docs_display = simple_fields_value('pageproduit'); $docs_current_product = simple_fields_fieldgroup('produits'); $docs_product = (implode(array_keys(array_filter($docs_current_product)))); // if we check the document to be on product page if( (strpos($docs_product, $product) !==false) && ($docs_display == 1) ) { // get the cat name $terms = get_the_terms($post->ID, 'docs_cat'); foreach ( $terms as $term ) { $cat = $term->name; } // PDF documents (attachments of posts) $args = array( 'post_mime_type' => 'application/pdf', 'post_type' => 'attachment', 'numberposts' => -1, 'post_parent' => $post->ID ); $attachments = get_posts($args); if ($attachments) { foreach ( $attachments as $attachment ) { echo '<li>'; the_post_thumbnail(array(70,98)); echo '<p>'; the_attachment_link( $attachment->ID , false ); $type = get_post_mime_type( $attachment->ID ); if( $type == 'application/pdf' ) : echo '<small>'.$cat.'<br />PDF'; endif; echo ' (' . size_format( filesize( get_attached_file( $attachment->ID ) ) ) . ')</small></p></li>'; } } else {} wp_reset_postdata(); // videos $args = array( 'cat' => '173'); $videos = get_posts($args); if ($videos) { foreach ($videos as $video) { echo '<li class="video">'; the_post_thumbnail(array(175,98)); echo '<p><a>'.the_title('','',false).'</a>'; echo '<small>'.$cat.'</small></p>'; echo '</li>'; } } else {} wp_reset_postdata(); } else {} // end of if we check the document to be on product page endwhile; wp_reset_postdata(); // end of while documents ?>
Viewing 9 replies - 1 through 9 (of 9 total)
Viewing 9 replies - 1 through 9 (of 9 total)
- The topic ‘Problem with nested loops’ is closed to new replies.