[Support request] How to apply three columns to a custom post type taxonomy

Home Forums Support [Support request] How to apply three columns to a custom post type taxonomy

Home Forums Support How to apply three columns to a custom post type taxonomy

Viewing 15 posts - 1 through 15 (of 20 total)
  • Author
    Posts
  • #1945474
    Takeru

    I am using the “Custom Post Type UI” plugin, and I want to apply 3 columns to the taxonomy of the custom post type, as shown in the code below.
    Can you please tell me exactly what code I need to add?
    Thanks!

    add_filter( 'generate_blog_columns','tu_products_columns' );
    function tu_products_columns( $columns ) {
        if ( is_post_type_archive( 'products' ) ) {
            return true;
        }
    
        return $columns;
    }
    #1945480
    David
    Staff
    Customer Support

    Hi there,

    try this:

    add_filter( 'generate_blog_columns', function( $columns ) {
        if ( 'your_cpt_slug' === get_post_type() && ! is_singular() ) {
            return true;
        }
    
        return $columns;
    } );

    Change the your_cpt_slug to the match your requirements.

    #1945491
    Takeru

    I tried that code, but it did not work.

    #1945564
    David
    Staff
    Customer Support

    Can you share a link to the archive page ?

    #1945814
    Takeru

    Please check.

    #1945817
    David
    Staff
    Customer Support

    I can’t see the link? Can you make sure its a valid URL.

    #1946374
    Takeru

    Can you see it now?

    #1946447
    Elvin
    Staff
    Customer Support

    Hi Takeru,

    You can try this PHP snippet:

    add_filter( 'generate_blog_columns','tu_products_columns' );
    function tu_products_columns( $columns ) {
        if ( is_post_type_archive( 'yumeshizuku' ) ) {
            return true;
        }
    
        return $columns;
    }
    #1946468
    Takeru

    Thank you.
    However, I am getting the following error message.

    The snippet has been deactivated due to an error on line 3:
    
    Cannot redeclare function tu_products_columns.
    #1946554
    Elvin
    Staff
    Customer Support

    That error is due to a reuse of the function callback.

    Can you try removing the previous code and use the one I’ve provided?

    #1946568
    Takeru

    I think I got an error because I already registered another code below.
    What code should I register to make both of them work?

    add_filter( 'generate_blog_columns','tu_products_columns' );
    function tu_products_columns( $columns ) {
        if ( is_post_type_archive( 'products' ) ) {
            return true;
        }
    
        return $columns;
    }
    #1946575
    Elvin
    Staff
    Customer Support

    I think I got an error because I already registered another code below.
    What code should I register to make both of them work?

    Yes that’s correct. You need to remove that code and keep the one I’ve provided. 😀

    #1946579
    Takeru

    It did not work well.
    Also, the page that was working originally could not be displayed in columns due to the removal of the code.

    #1946605
    Elvin
    Staff
    Customer Support

    Can you mention the Custom post type’s slug and the taxonomy slug used by the custom post type?

    Also, are you trying to apply this on product archive as well?

    If that’s the case then let’s just modify your existing code to this:

    add_filter( 'generate_blog_columns','tu_products_columns' );
    function tu_products_columns( $columns ) {
    
        $term = get_queried_object();   
        $taxonomy = $term->taxonomy;
        $termSlug = $term->slug;
    
        if ( is_post_type_archive( 'products' ) || $taxonomy == 'kind' && $termSlug == 'yumeshizuku'  ) {
            return true;
        }
    
        return $columns;
    }

    To explain this code:

    The condition is set to check if the current page is an archive page is of ‘product’ post type OR if the the current page has a term yumeshizuku under taxonomy kind

    #1946618
    Takeru

    It worked! Thank you!

    If I want to apply it to other taxonomies and slugs, what code do I need to write?
    For example, if I use “kind” and “area”, or “produsts” and “news”.

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