Complex custom post type multiple loops in WordPress
-
I have a custom post type for Staff Members of an organisation with a taxonomy named Profession. I’m using easytabs to display a matrix of photos for each member sorted into different professions (taxonomys). When a user clicks on the photo (the tab navigation), the appropriate information is displayed in the panel as it animates to view.
I can only fit 4 members in each tab container div across the page, any more and the 5th one breaks the tab layout.
The way easytabs is structured, I need firstly a
<ul>of nav items then followed by the corresponding tab panels. I have achieved this by using rewind_posts().I need a loop to pull only 4 Staff Members per tab-container div (see code below), then create a new tab container the next 4 etc.
This is the code I have so far …
<div class="team_content"> <?php $custom_terms = get_terms('profession'); foreach($custom_terms as $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 the profession echo '<div class="tab-container">'; echo '<ul>'; while($loop->have_posts()) : $loop->the_post(); //first sub-loop sets up the nav <ul> //extract field names from metaboxes $salutation = get_post_meta( $post->ID, '_cmb_salutation', true ); $full_title = $salutation.' '.get_the_title(); // create entire title eg Dr. John Smith $title = get_the_title(); $title_link = str_replace(' ','',$title); // remove spaces to make links work $final_title_link = strtolower($title_link); echo '<li><a href="#'.$final_title_link.'">'; the_post_thumbnail("team-member"); echo '<h4>'.$full_title.'</h4></a></li>'; endwhile; echo '</ul>'; rewind_posts(); echo '<div class="panel-container">'; while($loop->have_posts()) : $loop->the_post(); //second sub-loop sets up the tab panels //extract field names from 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 ); $title2 = get_the_title(); //this variable must be re-created for this sub loop $title_link2 = str_replace(' ','',$title2); //this variable must be re-created for this sub loop $final_title_link2 = strtolower($title_link2); //this variable must be re-created for this sub loop 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-container } ?> </div><!-- .team_content -->
The topic ‘Complex custom post type multiple loops in WordPress’ is closed to new replies.