Home › Forums › Support › How do I display custom post types in columns, not controlled by the customizer?
- This topic has 9 replies, 3 voices, and was last updated 2 years, 2 months ago by Ying.
-
AuthorPosts
-
February 18, 2022 at 10:54 am #2124554Mark
Using GP Premium and GB Pro
I created a custom post type with a taxonomy using CPT UI and ACF for the terms.
In GB I added a content template that I associated with the post type and the separate taxonomy terms. They display properly but only in one column.Using Code Snippets I added a function that I found in this support forum to display in columns with the columns controlled by the Customizer. Thank you Leo : https://generatepress.com/forums/topic/how-to-apply-three-columns-to-a-custom-post-type-taxonomy/page/2/
Everything works.
There’s one last tweak I’d like to make, but I don’t know if it’s possible.
Is there a function that handles the number of columns on the CPT, instead of the columns being controlled by the customizer? The use is, I want my blog to display in three columns, but I want my CPT to display in two columns.
February 18, 2022 at 10:55 am #2124561LeoStaffCustomer SupportHi there,
This should help:
https://docs.generatepress.com/article/using-columns-in-the-blog/#changing-the-number-of-columnsFebruary 18, 2022 at 11:17 am #2124588MarkThanks Leo – when I added the function I didn’t get the result. I set is the return to 50, for two columns, but it remained at three. I tested in the customizer just to make sure. Am I missing something? Do I need to add the name of the post type, and taxonomy terms?
Below is the function I adapted from your example. Would I need part of this?
add_filter( 'generate_blog_columns','tu_artisans_columns' ); function tu_artisans_columns( $columns ) { $term = get_queried_object(); $taxonomy = $term->taxonomy; $termSlug = $term->slug; $tax_list_included = array('artisan_types','art','clay'); if ( is_post_type_archive( 'artisans' ) || in_array($taxonomy, $tax_list_included) ) { return true; } return $columns; }
I tried this, but it didn’t work.
add_filter( 'generate_blog_get_column_count','tu_search_column_count' ); function tu_search_column_count( $count ) { if ( is_post_type_archive( 'artisan_types' ) ) { return 50; } return $count; }
February 18, 2022 at 11:31 am #2124603YingStaffCustomer SupportHi Mark,
What’s the code you use to set it to 2 columns?
February 18, 2022 at 11:41 am #2124610MarkOriginally I used this from the GP documentation;
add_filter( 'generate_blog_get_column_count','tu_search_column_count' ); function tu_search_column_count( $count ) { if ( is_search() ) { return 50; } return $count; }
February 18, 2022 at 12:25 pm #2124655LeoStaffCustomer SupportTry using Elvin’s format here:
https://generatepress.com/forums/topic/custom-post-types-break-column-in-categories/#post-1861993February 18, 2022 at 12:56 pm #2124687MarkThanks Leo. That works for the archive of the custom post type. I’ve got the two columns like I want. I know I’m supposed to have more CPTs in the array, but I only have one for this site.
My last hurdle is, I’m not sure how to include the taxonomies. The function works on the CPT, but not on the CPT’s taxonomies. I have a taxonomy called ‘artisan_types’, and the two types are ‘art’,’clay’.
This is the code I used to get two columns in the archive.
add_filter( 'generate_blog_columns','tu_portfolio_columns' ); function tu_portfolio_columns( $columns ) { if ( is_post_type_archive( array('artisans') ) ) { return true; } return $columns; } add_filter( 'generate_blog_get_column_count','tu_search_column_count' ); function tu_search_column_count( $count ) { if ( is_post_type_archive( array('artisans') ) ) { return 50; } return $count; }
February 18, 2022 at 4:08 pm #2124793YingStaffCustomer SupportTry change
is_post_type_archive( array('artisans') )
tois_post_type_archive( 'artisans') || is_tax()
in the both codes.February 18, 2022 at 5:07 pm #2124822MarkThanks Ying! It’s all working elegantly now. I have control over the number of columns in the CPT archive, as well as the archive for the CPT’s taxonomies. In case anyone else needs to figure this out this is what I used.
The first filter creates columns in the CPT archive, as well as the archive for the CPT’s taxonomy.
add_filter( 'generate_blog_columns','tu_portfolio_columns' ); function tu_portfolio_columns( $columns ) { if ( is_post_type_archive( 'artisans' ) || is_tax() ) { return true; } return $columns; }
The second filter creates the number of columns.
50 means 50%, which is 2 columns.
33 would be 33%, 3 columns.
20 would be 20%, 5 columns.add_filter( 'generate_blog_get_column_count','tu_search_column_count' ); function tu_search_column_count( $count ) { if ( is_post_type_archive( 'artisans' ) || is_tax() ) { return 50; } return $count; }
I also added this WordPress function to sort the CPTs by title.
add_action( 'pre_get_posts', 'my_change_sort_order'); function my_change_sort_order($query){ if(is_post_type_archive( $post_type ) || is_tax()): //Set the order ASC or DESC $query->set( 'order', 'ASC' ); //Set the orderby $query->set( 'orderby', 'title' ); endif; };
Thanks for all your help. You guys are great!
February 18, 2022 at 5:19 pm #2124835YingStaffCustomer SupportYou are welcome 🙂
Thanks for sharing!
-
AuthorPosts
- You must be logged in to reply to this topic.