Community Forums › Forums › Archived Forums › Design Tips and Tricks › Multiple Loops on Custom Post Type Archive Page
- This topic has 9 replies, 2 voices, and was last updated 7 years, 6 months ago by Brad Dalton.
-
AuthorPosts
-
October 10, 2016 at 2:01 pm #194475brettatkinMember
I have an archive page for a custom post type called "Work" where individual project information is saved. That CPT has a custom taxonomy ("Type") and each project is assigned to one of those Types.
I have three loops on the page, one for each project type (Dwell, Commerce and Interiors). The first grouping is correct. The second grouping duplicates each project twice. The third grouping duplicates each project three times.
I used the information on this page as a starting point: https://sridharkatakam.com/multiple-loops-genesis/
My code is below. I'm assuming it has something to do with not resetting the query and/or loop, but what I've tried doesn't work and the starting point example linked above doesn't do that.
http://dr.betaforming.com/work/projects/<?php //* Remove the ad widget remove_action( 'genesis_before_loop', 'adspace_before_loop' ); //* Remove author box remove_action( 'genesis_after_entry', 'genesis_do_author_box_single', 8 ); //* Remove the post meta function remove_action( 'genesis_entry_footer', 'genesis_post_meta' ); //* Remove the post info function remove_action( 'genesis_entry_header', 'genesis_post_info', 12 ); //* Remove the post image (requires HTML5 theme support) remove_action( 'genesis_entry_content', 'genesis_do_post_image', 8 ); //* Remove the entry title (requires HTML5 theme support) remove_action( 'genesis_entry_header', 'genesis_do_post_title' ); //* Remove the post content remove_action( 'genesis_entry_content', 'genesis_do_post_content' ); remove_action( 'genesis_loop', 'genesis_do_loop' ); add_action( 'genesis_loop', 'dk_work_loop' ); /** * Outputs a custom loop * * @global mixed $paged current page number if paginated * @return void */ function dk_work_loop() { // Loop 1 $args_1 = array( 'post_type' => 'work', 'order' => 'DESC', 'orderby' => 'date', 'post_status' => 'publish', 'tax_query' => array( array( 'taxonomy' => 'work-type', 'field' => 'slug', 'terms' => 'dwell', ), ), 'posts_per_page' => -1 ); echo '<h2>Dwell</h2>'; //* Add the featured image after post title add_action( 'genesis_entry_header', 'dr_work_grid_1' ); function dr_work_grid_1() { if ( has_post_thumbnail() ){ echo '<div class="archive-featured-image">'; echo '<a href="' . get_permalink() .'" title="' . the_title_attribute( 'echo=0' ) . '">'; echo get_the_post_thumbnail($thumbnail->ID); echo '</a>'; echo '<a href="' . get_permalink() .'" title="' . the_title_attribute( 'echo=0' ) . '">'; echo the_title( '<p class="work-title">', '</p>' ); echo '</a>'; echo '</div>'; } } genesis_custom_loop( $args_1 ); // Loop 2 $args_2 = array( 'post_type' => 'work', 'order' => 'DESC', 'orderby' => 'date', 'post_status' => 'publish', 'tax_query' => array( array( 'taxonomy' => 'work-type', 'field' => 'slug', 'terms' => 'commerce', ), ), 'posts_per_page' => -1 ); echo '<h2 style="clear: both;">Commerce</h2>'; //* Add the featured image after post title add_action( 'genesis_entry_header', 'dr_work_grid_2' ); function dr_work_grid_2() { if ( has_post_thumbnail() ){ echo '<div class="archive-featured-image">'; echo '<a href="' . get_permalink() .'" title="' . the_title_attribute( 'echo=0' ) . '">'; echo get_the_post_thumbnail($thumbnail->ID); echo '</a>'; echo '<a href="' . get_permalink() .'" title="' . the_title_attribute( 'echo=0' ) . '">'; echo the_title( '<p class="work-title">', '</p>' ); echo '</a>'; echo '</div>'; } } genesis_custom_loop( $args_2 ); // Loop 3 $args_3 = array( 'post_type' => 'work', 'order' => 'DESC', 'orderby' => 'date', 'post_status' => 'publish', 'tax_query' => array( array( 'taxonomy' => 'work-type', 'field' => 'slug', 'terms' => 'interiors', ), ), 'posts_per_page' => -1 ); echo '<h2 style="clear: both;">Interiors</h2>'; //* Add the featured image after post title add_action( 'genesis_entry_header', 'dr_work_grid_3' ); function dr_work_grid_3() { if ( has_post_thumbnail() ){ echo '<div class="archive-featured-image">'; echo '<a href="' . get_permalink() .'" title="' . the_title_attribute( 'echo=0' ) . '">'; echo get_the_post_thumbnail($thumbnail->ID); echo '</a>'; echo '<a href="' . get_permalink() .'" title="' . the_title_attribute( 'echo=0' ) . '">'; echo the_title( '<p class="work-title">', '</p>' ); echo '</a>'; echo '</div>'; } } genesis_custom_loop( $args_3 ); } genesis();
October 11, 2016 at 2:58 pm #194538Brad DaltonParticipantDid you want to display entries from 3 different taxonomy types on the one page?
Also, you haven't defined a value for the $thumbnail variable.
Isn't this the URL you should be working with http://dr.betaforming.com/work/
October 11, 2016 at 3:28 pm #194540brettatkinMemberYes, I want to display all the projects broken down by category (Dwell, Commerce and Interiors) - the one taxonomy (work-type) has three categories.
Not sure what you mean by the $thumbnail variable. Why do I need to define that?
Yes, ultimately the url will be /work. I had the slug configured for my CPT to be work/projects just so I could keep the other working example. I have removed that page and updated my CPT slug to be what it should be in the end. So the new URL is:
http://dr.betaforming.com/work/
Thanks
October 11, 2016 at 3:36 pm #194541Brad DaltonParticipantYou'd need to post all your code for the CPT and taxonomy.
So there's only 1 taxonomy type? And whats its name?
But you added 3 categories.
The only way to test this is to text all code.
Any reason you couldn't simply create 3 taxonomy types? Best to use tax types with a CPT rather than categories. You'll find the code for this in the Executive Pro theme.
October 11, 2016 at 4:12 pm #194542brettatkinMemberHere is the code for the CPT and CPT Taxonomy. I'm mixing categories (in the WP sense) with my general categorization scheme for the projects. I see why you're confused. Here is a screenshot of my taxonomy setup.
http://dr.betaforming.com/work-types.gif
Here is the code for my CPT and CPT Taxonomy.
//* Create Work custom taxonomy add_action( 'init', 'executive_type_taxonomy' ); function executive_type_taxonomy() { register_taxonomy( 'work-type', 'work', array( 'labels' => array( 'name' => _x( 'Types', 'taxonomy general name', 'executive' ), 'add_new_item' => __( 'Add New Work Type', 'executive' ), 'new_item_name' => __( 'New Work Type', 'executive' ), ), 'exclude_from_search' => true, 'has_archive' => true, 'hierarchical' => true, 'rewrite' => array( 'slug' => 'work-type', 'with_front' => false ), 'show_admin_column' => true, 'show_ui' => true, 'show_tagcloud' => false, ) ); } //* Create Work custom post type add_action( 'init', 'work_post_type' ); function work_post_type() { register_post_type( 'work', array( 'labels' => array( 'name' => __( 'Work', 'executive' ), 'singular_name' => __( 'Work', 'executive' ), ), 'has_archive' => true, 'hierarchical' => true, 'menu_icon' => 'dashicons-images-alt2', 'public' => true, 'rewrite' => array( 'slug' => 'work', 'with_front' => false ), 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'revisions', 'page-attributes', 'genesis-seo', 'genesis-cpt-archives-settings' ), 'taxonomies' => array( 'work-type' ), ) ); }
October 12, 2016 at 2:17 pm #194599Brad DaltonParticipantOctober 12, 2016 at 2:24 pm #194600brettatkinMemberarchive-work.php
October 15, 2016 at 6:37 am #194777brettatkinMemberHere is the solution that worked for me. It is what I expected I needed to do - I let the initial solution I found control my thinking rather than looking for another solution. Thanks to Carrie Dills (as usual) for getting me back on track. Here is the post form Carrie: https://carriedils.com/custom-page-template-genesis/
Here is my final code:
<?php //* Remove the ad widget remove_action( 'genesis_before_loop', 'adspace_before_loop' ); //* Remove author box remove_action( 'genesis_after_entry', 'genesis_do_author_box_single', 8 ); //* Remove the post meta function remove_action( 'genesis_entry_footer', 'genesis_post_meta' ); //* Remove the post info function remove_action( 'genesis_entry_header', 'genesis_post_info', 12 ); //* Remove the post image (requires HTML5 theme support) remove_action( 'genesis_entry_content', 'genesis_do_post_image', 8 ); //* Remove the entry title (requires HTML5 theme support) remove_action( 'genesis_entry_header', 'genesis_do_post_title' ); //* Remove the post content remove_action( 'genesis_entry_content', 'genesis_do_post_content' ); remove_action( 'genesis_loop', 'genesis_do_loop' ); add_action( 'genesis_loop', 'dr_work_loop' ); function dr_work_loop() { $args_1 = (array( 'post_type' => 'work', 'order' => 'DESC', 'orderby' => 'date', 'post_status' => 'publish', 'tax_query' => array( array( 'taxonomy' => 'work-type', 'field' => 'slug', 'terms' => 'dwell', ), ), 'posts_per_page' => -1 )); $loop = new WP_Query( $args_1 ); if( $loop->have_posts() ) { // loop through posts echo '<h2 id="dwell">Dwell</h2>'; while( $loop->have_posts() ): $loop->the_post(); echo '<div class="archive-featured-image type-work">'; echo '<a href="' . get_permalink() .'" title="' . the_title_attribute( 'echo=0' ) . '">'; echo get_the_post_thumbnail($thumbnail->ID); echo '</a>'; echo '<a href="' . get_permalink() .'" title="' . the_title_attribute( 'echo=0' ) . '">'; echo the_title( '<p class="work-title">', '</p>' ); echo '</a>'; echo '</div>'; endwhile; echo '<div style="clear: both;"></div>'; } wp_reset_postdata(); $args_2 = (array( 'post_type' => 'work', 'order' => 'DESC', 'orderby' => 'date', 'post_status' => 'publish', 'tax_query' => array( array( 'taxonomy' => 'work-type', 'field' => 'slug', 'terms' => 'commerce', ), ), 'posts_per_page' => -1 )); $loop = new WP_Query( $args_2 ); if( $loop->have_posts() ) { // loop through posts echo '<h2 id="commerce">Commerce</h2>'; while( $loop->have_posts() ): $loop->the_post(); echo '<div class="archive-featured-image type-work">'; echo '<a href="' . get_permalink() .'" title="' . the_title_attribute( 'echo=0' ) . '">'; echo get_the_post_thumbnail($thumbnail->ID); echo '</a>'; echo '<a href="' . get_permalink() .'" title="' . the_title_attribute( 'echo=0' ) . '">'; echo the_title( '<p class="work-title">', '</p>' ); echo '</a>'; echo '</div>'; endwhile; echo '<div style="clear: both;"></div>'; } wp_reset_postdata(); $args_3 = (array( 'post_type' => 'work', 'order' => 'DESC', 'orderby' => 'date', 'post_status' => 'publish', 'tax_query' => array( array( 'taxonomy' => 'work-type', 'field' => 'slug', 'terms' => 'Interiors', ), ), 'posts_per_page' => -1 )); $loop = new WP_Query( $args_3 ); if( $loop->have_posts() ) { // loop through posts echo '<h2 id="interiors">Interiors</h2>'; while( $loop->have_posts() ): $loop->the_post(); echo '<div class="archive-featured-image type-work">'; echo '<a href="' . get_permalink() .'" title="' . the_title_attribute( 'echo=0' ) . '">'; echo get_the_post_thumbnail($thumbnail->ID); echo '</a>'; echo '<a href="' . get_permalink() .'" title="' . the_title_attribute( 'echo=0' ) . '">'; echo the_title( '<p class="work-title">', '</p>' ); echo '</a>'; echo '</div>'; endwhile; } wp_reset_postdata(); } genesis();
October 15, 2016 at 7:06 am #194779Brad DaltonParticipantAnother solution:
1. I suggest you use foreach with 1 function as its far more efficient.
2. I'd also suggest using genesis_get_image rather than get_the_post_thumbnail
3. And printf for the output rather than echo.
I know that code still works but it's a huge amount of code just to display a loop of CPT entries foreach taxonomy type or category.
You wouldn't want to use a new WP_Query for every single taxonomy.
October 15, 2016 at 8:44 am #194785Brad DaltonParticipantSomething like this but swap out query_posts with get_posts or WP_Query and modify the parameters to match your taxonomies and CPT.
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.