Site logo

[Resolved] Custom Post Types Break Column in Categories

Home Forums Support [Resolved] Custom Post Types Break Column in Categories

Home Forums Support Custom Post Types Break Column in Categories

Viewing 15 posts - 1 through 15 (of 19 total)
  • Author
    Posts
  • #1861733
    Ken

    Hi, Recently I had my coach setup four custom posts types (CPT). So far the post look good when published.
    Problem: When I go to a Category that was assigned to a CPT, instead of seeing the Category Post in 3-columns, I see only one long column. See attached screenshot.

    Is there some kind of magic coding I can do to fix this so that the Category Posts show up in 3-columns?

    Appreciate any helpful suggestions.
    link to Category URL
    Link to Screenshot
    Temporary login Link

    #1861938
    Ken

    UPDATE: I moved all the CPT to a new category see link below.
    You can see each custom posts is in one column. there should be three columns. You can see three columns if you click on any other catogeries.

    I’m still researching an answer.

    #1861950
    Ken

    Hi again, apologies. I meant to tell you what my coach did. She sent me the following:

    When I assign a custom post type to a category it removes the column layout for that category page. I followed the steps in this post to add columns code to the child theme’s functions.php file but it doesn’t seem to work: https://docs.generatepress.com/article/using-columns-in-the-blog/#adding-columns-to-your-custom-post-type. Maybe the code is supposed to be inside the custom post template instead? Can you check with support?

    I hope this helps with the troubleshooting.

    Please let me know if the code should be inside the custom post template, and if so can you kindly show me where I should put the code? You’ll only need to show me once and I can then do the same for the three other custom post types. Thank you.

    #1861962
    Elvin
    Staff
    Customer Support

    Hi Ken,

    You can sort this out by using few filter.

    Check our documentation here – https://docs.generatepress.com/article/using-columns-in-the-blog/

    #1861969
    Ken

    Hi Elvin,
    I guess you didn’t see my screenshot. I selected 3 columns but only when I added the custom posts to a category, then navigate to the category it would show all posts in one column. It should be three. No problem with regular non-custom posts, they show up in three columns.

    I’m not sure how to fix this.

    #1861973
    Ken

    If you’re referring to this:

    Adding columns to your custom post type
    By default, columns only apply to the post post type. However, you can add columns to your custom post type using the filter:

    add_filter( ‘generate_blog_columns’,’tu_portfolio_columns’ );
    function tu_portfolio_columns( $columns ) {
    if ( is_post_type_archive( ‘portfolio’ ) ) {
    return true;
    }

    return $columns;
    }

    I’d like to know where I should place this filter? It sounds like I should add it to each of the four custom posts that I have–Is that correct?
    If so,
    Not sure where to place the code.

    #1861987
    Ken

    Ahh, sorry, forgot to add the following;
    If it is the case where I should add the filter to the custom post
    Let’s try an example:
    In the child theme there’s
    single-one_product.php
    and
    content-one_product.php

    Questions:
    1) which one (single or content) should I put the code in?
    2) where in ‘that one (single or content)” should I paste the code?

    Apologies for all these posts. I appreciate your help, understanding and above all patience with me.

    #1861990
    Ken

    This is what I have in my ‘functions.php’ file:

    <?php
    /**
    * GeneratePress child theme functions and definitions.
    *
    * Add your custom PHP in this file.
    * Only edit this file if you have direct access to it on your server (to fix errors if they happen).
    */

    add_filter( ‘generate_blog_columns’,’tu_portfolio_columns’ );
    function tu_portfolio_columns( $columns ) {
    if ( is_post_type_archive( ‘list_post’,’one_product’,’ten_product’,’five_product’,’how_to’ ) ) {
    return true;
    }

    return $columns;
    }

    add_filter( ‘generate_blog_masonry’,’tu_portfolio_masonry’ );
    function tu_portfolio_masonry( $masonry ) {
    if ( is_post_type_archive( ‘list_post’,’one_product’,’ten_product’,’five_product’,’how_to’ ) ) {
    return ‘true’;
    }

    return $masonry;
    }

    function my_cptui_add_post_types_to_archives( $query ) {
    // We do not want unintended consequences – also modified to include home page.
    if ( is_admin() || ! $query->is_main_query() ) {
    return;
    }

    if ( is_category() || is_tag() || is_home() && empty( $query->query_vars[‘suppress_filters’] ) ) {
    $cptui_post_types = cptui_get_post_type_slugs();

    $query->set(
    ‘post_type’,
    array_merge(
    array( ‘post’ ),
    $cptui_post_types
    )
    );
    }
    }
    add_filter( ‘pre_get_posts’, ‘my_cptui_add_post_types_to_archives’ );

    add_filter(‘widget_posts_args’, ‘widget_posts_args_add_custom_type’);
    function widget_posts_args_add_custom_type($params) {
    $params[‘post_type’] = array(‘post’,’one_product’,’ten_product’,’five_product’,’list_post’,’how_to’);
    return $params;
    }

    function my_cptui_add_post_types_to_rss( $query ) {
    // We do not want unintended consequences.
    if ( ! $query->is_feed() ) {
    return;
    }

    $cptui_post_types = cptui_get_post_type_slugs();

    $query->set(
    ‘post_type’,
    array_merge(
    array( ‘post’ ),
    $cptui_post_types
    )
    );
    }
    add_filter( ‘pre_get_posts’, ‘my_cptui_add_post_types_to_rss’ );

    #1861993
    Elvin
    Staff
    Customer Support

    Perhaps I’m not understanding the structure clearly.

    To clarify: You want custom post type archives to have 3 columns as well?

    If so, then the generate_blog_columns filter you’ve spotted is the way to go.

    You place that on a child themes functions.php or a code snippets plugin.

    Here’s a modified example for 4 cpts.

    add_filter( 'generate_blog_columns','tu_portfolio_columns' );
    function tu_portfolio_columns( $columns ) {
        if ( is_post_type_archive( array('cpt1','cpt2','cpt3','cpt4') ) ) {
            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('cpt1','cpt2','cpt3','cpt4') ) ) {
            return 33;
        }
    
        return $count;
    }

    You basically change cpt1, cpt2, cpt3, cpt4 with your cpt slugs. 😀

    #1862002
    Ken

    Ah, it’s not working. Here’s what I did:

    <?php
    /**
    * GeneratePress child theme functions and definitions.
    *
    * Add your custom PHP in this file.
    * Only edit this file if you have direct access to it on your server (to fix errors if they happen).
    */

    add_filter( ‘generate_blog_columns’,’tu_portfolio_columns’ );
    function tu_portfolio_columns( $columns ) {
    if ( is_post_type_archive( array(‘list_post’,’one_product’,’ten_product’,’five_product’,’how_to’ ) ) ){
    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(‘list_post’,’one_product’,’ten_product’,’five_product’,’how_to’) ) ) {
    return 33;
    }

    return $count;
    }
    add_filter( ‘generate_blog_masonry’,’tu_portfolio_masonry’ );
    function tu_portfolio_masonry( $masonry ) {
    if ( is_post_type_archive( ‘list_post’,’one_product’,’ten_product’,’five_product’,’how_to’ ) ) {
    return ‘true’;
    }

    return $masonry;
    }

    function my_cptui_add_post_types_to_archives( $query ) {
    // We do not want unintended consequences – also modified to include home page.
    if ( is_admin() || ! $query->is_main_query() ) {
    return;
    }

    if ( is_category() || is_tag() || is_home() && empty( $query->query_vars[‘suppress_filters’] ) ) {
    $cptui_post_types = cptui_get_post_type_slugs();

    $query->set(
    ‘post_type’,
    array_merge(
    array( ‘post’ ),
    $cptui_post_types
    )
    );
    }
    }
    add_filter( ‘pre_get_posts’, ‘my_cptui_add_post_types_to_archives’ );

    add_filter(‘widget_posts_args’, ‘widget_posts_args_add_custom_type’);
    function widget_posts_args_add_custom_type($params) {
    $params[‘post_type’] = array(‘post’,’one_product’,’ten_product’,’five_product’,’list_post’,’how_to’);
    return $params;
    }

    function my_cptui_add_post_types_to_rss( $query ) {
    // We do not want unintended consequences.
    if ( ! $query->is_feed() ) {
    return;
    }

    $cptui_post_types = cptui_get_post_type_slugs();

    $query->set(
    ‘post_type’,
    array_merge(
    array( ‘post’ ),
    $cptui_post_types
    )
    );
    }
    add_filter( ‘pre_get_posts’, ‘my_cptui_add_post_types_to_rss’ );

    #1862003
    Ken

    Forgot to confirm: yes, I would love to have 3-columns for custom post types.

    #1862134
    Elvin
    Staff
    Customer Support

    Strange.

    Can you link me to a category archive page of a custom post type and a category archive page of a default page.

    The archive linked on the private information text field doesn’t seem to be an archive for CPT category. (I’ve clicked a post on it and the post type was a default post.)

    #1862194
    Ken

    I’ve attached the links below.

    #1862485
    David
    Staff
    Customer Support

    Hi there,

    that condition checks for a custom post type archive, where it looks like you’re just displaying a CPT in a default archive.
    Try this snippet instead:

    add_filter( 'generate_blog_columns', function( $columns ) {
        if ( in_array(get_post_type(), array('cpt-1','cpt-2','cpt-3')) && ! is_singular() ) {
            return true;
        }
    
        return $columns;
    } );

    Update the array() to your CPT slugs.

    #1863160
    Ken

    Hi David, bummers, it didn’t work. Here’s how my Functions.php looks after adding the code you gave me.
    I’ve attached a VDO cast. I may sound a bit slow, it’s 1am here in Japan.

    <?php
    /**
    * GeneratePress child theme functions and definitions.
    *
    * Add your custom PHP in this file.
    * Only edit this file if you have direct access to it on your server (to fix errors if they happen).
    */

    add_filter( ‘generate_blog_columns’, function( $columns ) {
    if ( in_array(get_post_type(), array(‘list_post’,’one_product’,’ten_product’,’five_product’,’how_to’)) && ! is_singular() ) {
    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(‘list_post’,’one_product’,’ten_product’,’five_product’,’how_to’) ) ) {
    return 33;
    }

    return $count;
    }

    add_filter( ‘generate_blog_masonry’,’tu_portfolio_masonry’ );
    function tu_portfolio_masonry( $masonry ) {
    if ( is_post_type_archive( ‘list_post’,’one_product’,’ten_product’,’five_product’,’how_to’ ) ) {
    return ‘true’;
    }

    return $masonry;
    }

    function my_cptui_add_post_types_to_archives( $query ) {
    // We do not want unintended consequences – also modified to include home page.
    if ( is_admin() || ! $query->is_main_query() ) {
    return;
    }

    if ( is_category() || is_tag() || is_home() && empty( $query->query_vars[‘suppress_filters’] ) ) {
    $cptui_post_types = cptui_get_post_type_slugs();

    $query->set(
    ‘post_type’,
    array_merge(
    array( ‘post’ ),
    $cptui_post_types
    )
    );
    }
    }
    add_filter( ‘pre_get_posts’, ‘my_cptui_add_post_types_to_archives’ );

    add_filter(‘widget_posts_args’, ‘widget_posts_args_add_custom_type’);
    function widget_posts_args_add_custom_type($params) {
    $params[‘post_type’] = array(‘post’,’one_product’,’ten_product’,’five_product’,’list_post’,’how_to’);
    return $params;
    }

    function my_cptui_add_post_types_to_rss( $query ) {
    // We do not want unintended consequences.
    if ( ! $query->is_feed() ) {
    return;
    }

    $cptui_post_types = cptui_get_post_type_slugs();

    $query->set(
    ‘post_type’,
    array_merge(
    array( ‘post’ ),
    $cptui_post_types
    )
    );
    }
    add_filter( ‘pre_get_posts’, ‘my_cptui_add_post_types_to_rss’ );

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