Archived topic
How to apply three columns to a custom post type taxonomy
19 replies · Started by Takeru on September 28, 2021
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;
}
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.
I tried that code, but it did not work.
Can you share a link to the archive page ?
Please check.
I can't see the link? Can you make sure its a valid URL.
Can you see it now?
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;
}
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.
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?
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;
}
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. :D
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.
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
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".