Complex custom post type multiple loops in Wordpress -
i have custom post type staff members of organisation taxonomy named profession. i'm using easytabs display matrix of photos each member sorted different professions. when user clicks on photo (the tab navigation), appropriate information displayed in panel animates view.
i can fit 4 members in each tab container div across page, more , 5th 1 breaks tab layout.
i need loop pull 4 staff members per container, next 4 etc.
this code have far ...
<div class="team_content"> <?php $custom_terms = get_terms('profession'); foreach($custom_terms $custom_term) { wp_reset_query(); $args = array('post_type' => 'team_members', 'tax_query' => array( array( 'taxonomy' => 'profession', 'field' => 'slug', 'terms' => $custom_term->slug, ), ), ); $loop = new wp_query($args); if($loop->have_posts()) { echo '<h2>'.$custom_term->name.'</h2>'; //displays profession echo '<div class="tab-collapsible-container">'; echo '<ul>'; while($loop->have_posts()) : $loop->the_post(); //first sub-loop //extract field names metaboxes $salutation = get_post_meta( $post->id, '_cmb_salutation', true ); $title = $salutation.' '.get_the_title(); $full_title = get_the_title(); $title_link = str_replace(' ','',$full_title); $final_title_link = strtolower($title_link); echo '<li><a href="#'.$final_title_link.'">'; the_post_thumbnail("team-member"); echo '<h4>'.$title.'</h4></a></li>'; endwhile; echo '</ul>'; rewind_posts(); echo '<div class="panel-container">'; while($loop->have_posts()) : $loop->the_post(); //extract field names metaboxes $profession = get_post_meta( $post->id, '_cmb_profession', true ); $qualifications = get_post_meta( $post->id, '_cmb_qualifications', true ); $services_url = get_post_meta( $post->id, '_cmb_services_url', true ); $full_title2 = get_the_title(); $title_link2 = str_replace(' ','',$full_title2); $final_title_link2 = strtolower($title_link2); echo '<div id="'.$final_title_link2.'" class="member_info">'; echo '<h4>'.$profession.' '.$qualifications.'</h4>'; the_content(); echo '<a href="'.$services_url.'" class="button2">visit service page</a>'; //services page link echo '</div>'; endwhile; echo '</div>'; //panel-container } echo '</div>'; //tab-collapsible-container } ?> </div><!-- .team_content -->
thank @anstrangel0ver quick reply! have tried counter & % operator on loop no success. i'm thinking more along lines of following code perishable press follows ...
// first loop: display posts 1 thru 5 <?php query_posts('showposts=5'); ?> <?php $posts = get_posts('numberposts=5&offset=0'); foreach ($posts $post) : start_wp(); ?> <?php static $count1 = 0; if ($count1 == "5") { break; } else { ?> <?php the_title(); ?> <?php the_content(); ?> <?php $count1++; } ?> <?php endforeach; ?> // second loop: display posts 6 thru 10 <?php query_posts('showposts=5'); ?> <?php $posts = get_posts('numberposts=5&offset=5'); foreach ($posts $post) : start_wp(); ?> <?php static $count2 = 0; if ($count2 == "5") { break; } else { ?> <?php the_title(); ?> <?php the_content(); ?> <?php $count2++; } ?> <?php endforeach; ?>
// third loop: etc ..................
just not sure how apply loop , keep right.
yikes, messy business rewind_posts()
in there. first i'd make function can advance custom wp_query number of posts:
function offset_posts($loop, $offset) { $post_counter = 0; while ($loop->have_posts() && ($post_counter < $offset) { $loop->next_post(); $post_counter++; } }
then pull staff member output code function can write 1 block of 4 staff members @ time:
function output_staff_members($loop, $offset, $number_to_output = 4) { // skip posts until $offset offset_posts($loop, $offset); $post_counter = 0; while ($loop->have_posts() && ($post_counter < $number_to_output)) : //first sub-loop $loop->the_post(); $post_counter++; //extract field names metaboxes $salutation = get_post_meta( $post->id, '_cmb_salutation', true ); $title = $salutation.' '.get_the_title(); $full_title = get_the_title(); $title_link = str_replace(' ','',$full_title); $final_title_link = strtolower($title_link); echo '<li><a href="#'.$final_title_link.'">'; the_post_thumbnail("team-member"); echo '<h4>'.$title.'</h4></a></li>'; endwhile; echo '</ul>'; $loop->rewind_posts(); offset_posts($loop, $offset); echo '<div class="panel-container">'; $post_counter = 0; while ($loop->have_posts() && ($post_counter < $number_to_output)) : //second sub-loop $loop->the_post(); $post_counter++; //extract field names metaboxes $profession = get_post_meta( $post->id, '_cmb_profession', true ); $qualifications = get_post_meta( $post->id, '_cmb_qualifications', true ); $services_url = get_post_meta( $post->id, '_cmb_services_url', true ); $full_title2 = get_the_title(); $title_link2 = str_replace(' ','',$full_title2); $final_title_link2 = strtolower($title_link2); echo '<div id="'.$final_title_link2.'" class="member_info">'; echo '<h4>'.$profession.' '.$qualifications.'</h4>'; the_content(); echo '<a href="'.$services_url.'" class="button2">visit service page</a>'; //services page link echo '</div>'; endwhile; echo '</div>'; //panel-container return $post_counter; }
lastly you'll need call new function enough times output staff members:
<div class="team_content"> <?php $custom_terms = get_terms('profession'); foreach($custom_terms $custom_term) { wp_reset_query(); $args = array('post_type' => 'team_members', 'tax_query' => array( array( 'taxonomy' => 'profession', 'field' => 'slug', 'terms' => $custom_term->slug, ), ), ); $loop = new wp_query($args); if($loop->have_posts()) { echo '<h2>'.$custom_term->name.'</h2>'; //displays profession echo '<div class="tab-collapsible-container">'; echo '<ul>'; // new code here: $post_counter = 0; while ($post_counter < $loop->$post_count) { $post_counter += output_staff_members($loop, $post_counter); } } echo '</div>'; //tab-collapsible-container } ?> </div><!-- .team_content -->
hope works you!
Comments
Post a Comment