- This topic has 19 replies, 3 voices, and was last updated 4 years, 8 months ago by
Elvin.
-
AuthorPosts
-
September 28, 2021 at 6:00 am #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; }September 28, 2021 at 6:10 am #1945480David
StaffCustomer SupportHi 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_slugto the match your requirements.September 28, 2021 at 6:25 am #1945491Takeru
I tried that code, but it did not work.
September 28, 2021 at 7:28 am #1945564David
StaffCustomer SupportCan you share a link to the archive page ?
September 28, 2021 at 8:00 am #1945814Takeru
Please check.
September 28, 2021 at 8:02 am #1945817David
StaffCustomer SupportI can’t see the link? Can you make sure its a valid URL.
September 28, 2021 at 11:25 pm #1946374Takeru
Can you see it now?
September 29, 2021 at 1:05 am #1946447Elvin
StaffCustomer SupportHi 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; }September 29, 2021 at 1:34 am #1946468Takeru
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.September 29, 2021 at 3:01 am #1946554Elvin
StaffCustomer SupportThat error is due to a reuse of the function callback.
Can you try removing the previous code and use the one I’ve provided?
September 29, 2021 at 3:08 am #1946568Takeru
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; }September 29, 2021 at 3:13 am #1946575Elvin
StaffCustomer SupportI 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. 😀
September 29, 2021 at 3:17 am #1946579Takeru
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.September 29, 2021 at 3:41 am #1946605Elvin
StaffCustomer SupportCan 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
yumeshizukuunder taxonomykindSeptember 29, 2021 at 3:51 am #1946618Takeru
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”. -
AuthorPosts
- You must be logged in to reply to this topic.