Archived topic
Custom post type archive layout questions
9 replies · Started by David on October 21, 2022
Hello
I've set up a custom post type called products.
I've then set up a block element using the Element Type : Content template
This works fine and products are showing up...
https://www.alfies-antiques.com/products/
I added this code so the custom post type shows in 3 columns
//ADD COLUMNS TO CUSTOM POST TYPES
add_filter( 'generate_blog_columns', function( $columns ) {
if ( 'products' === get_post_type() && ! is_singular() ) {
return true;
}
return $columns;
} );
My first questions is, how do I get it to show 2 columns on mobile?
My second questions is, why do the products not show up on standard taxonomy category pages...
https://www.alfies-antiques.com/category/handles-hooks/
Thanks
Dave
Hi there,
1. you can use this CSS to force the CPT columns to 2 on mobile:
@media (max-width: 768px) {
.archive .generate-columns {
width: 50% !important;
}
.archive .generate-columns {
padding-left: 20px !important;
}
}
2. Category archives will only display the post post-type by default.
Use the pre_get_posts hook to add others:
function db_multi_post_type_categories($query)
{
if($query->is_main_query() && is_category()){
$query->set('post_type', ['post', 'your-post-type']);
return;
}
}
add_action('pre_get_posts', 'db_multi_post_type_categories');
Thanks David - how do I show 2 columns on the post category archive page?
I edited the CSS above to apply to all archives.
Thanks David
You're welcome
Hi David
So I've updated my custom post type archive layout, by setting up a new Element Block and using the new Element Type > Loop template, which is great by the way.
Is it possible for it to use the loop template for taxonomy category pages though? So for example this category only has one product assigned to it, but it's showing all custom post type products...
Is it possible to set up a new loop template, that shows products per category? I don't want to be setting it up loops for each individual category though.
https://www.alfies-antiques.com/category/desks/
Thanks
Dave
So for the Loop Element - its designed to use the Inherit Query from Template option in the Query Loop Block.
Then if you set your Display Rules to eg. Category Archive, it will pull in just the posts that match that terms of the archive page the user is viewing.
Thanks David, that's great - loving the new loop template
Awesome - glad to hear that!