[Resolved] How do I display custom post types in columns, not controlled by the customizer?

Home Forums Support [Resolved] How do I display custom post types in columns, not controlled by the customizer?

Home Forums Support How do I display custom post types in columns, not controlled by the customizer?

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #2124554
    Mark

    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.

    #2124561
    Leo
    Staff
    Customer Support
    #2124588
    Mark

    Thanks 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;
    }
    #2124603
    Ying
    Staff
    Customer Support

    Hi Mark,

    What’s the code you use to set it to 2 columns?

    #2124610
    Mark

    Originally 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;
    }
    #2124655
    Leo
    Staff
    Customer Support
    #2124687
    Mark

    Thanks 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;
    }
    #2124793
    Ying
    Staff
    Customer Support

    Try change is_post_type_archive( array('artisans') ) to is_post_type_archive( 'artisans') || is_tax() in the both codes.

    #2124822
    Mark

    Thanks 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!

    #2124835
    Ying
    Staff
    Customer Support

    You are welcome 🙂

    Thanks for sharing!

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.