Archived topic
Custom Post Type – can't get columns to work
7 replies · Started by Simon on October 15, 2019
Hi Generate Press Team,
Big fan of your theme. I have a problem that I hope you can help with.
I have a site that is using a Custom Post Type (boats).
I am trying to get the Custom Post Type to appear in columns on the archive page for that post type, in the same way that the 'normal' post types do.
I have 'normal' posts set to columns-2 and that works fine. But the custom post type (boats) still shows as one column.
I have found the filter that you supply to fix this, but I can't get it to work. I am using the Code Snippets plugin as you recommend.
I am using the following snippet of PHP to get the custom post type (boats) to 'show' on the website. This worked fine.
==========
function namespace_add_custom_types( $query ) {
if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
$query->set( 'post_type', array(
'post', 'nav_menu_item', 'boats'
));
return $query;
}
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types' );
===========
I have also added the following filter from the Generate Press website. With a few changes so that it refers to my custom post type. But it doesn't generate columns.
===========
add_filter( 'generate_blog_columns','sf_boats_columns' );
function sf_boats_columns( $columns ) {
if ( is_post_type_archive( 'boats' ) ) {
return true;
}
return $columns;
}
===========
I have de-activated all plugins and tried switching theme, still doesn't work.
The site is being developed locally so I cannot share a url.
Thank you for any help with this.
PS. Just had a thought. The archive pages I am trying to get to display in Columns are all actually Category archives within a Custom Post Type. Rather than just the archive of the parent Custom Post Type.
eg: Custom Post Type = 'boats'. Categories within 'boats' = 'bluewater-cruiser', 'centreboard' etc.
It is the category archive page that I am trying to target with the make columns filter. So I need 'bluewater-cruiser' archive to be in columns. I hope that is clear. Thanks again for any help.
Hi there,
Your code looks good:
add_filter( 'generate_blog_columns', function( $columns ) {
if ( is_post_type_archive( 'boats' ) ) {
return true;
}
return $columns;
} );
I would need to actually see the site in order to figure out what's going on. Any chance you can put it up on a temp live server quickly?
Thank you for your swift reply Tom. I will see if I can 'make it live' briefly and get back to you.
Hi Tom, I have put the site up on a subdomain. How do I supply you with url, username and password? Thank you again for this. GeneratePress rocks!
Hi there,
you can send the URL and login info via the Account Issue form:
https://generatepress.com/contact/
Please add the URL for this topic to the form so we can track.
It seems you need to target categories and tags, instead of a cpt archive.
Let's try this:
add_filter( 'generate_blog_columns', function( $columns ) {
$targets = array(
'bluewater-cruiser',
'centreboard',
);
if ( is_archive( $targets ) ) {
return true;
}
return $columns;
} );
You just need to add more items to the $targets array.
Let me know if you need more info :)
Thank you Tom.
Works perfectly.
You have made my day.
Glad I could help! :)