Archived topic
Feature request... Custom post type support in Customiser
8 replies · Started by Andrew on April 16, 2021
Hey folks
Love the theme. I have one request that should be easy to implement. We can currently set options in the Customiser for posts and Pages but what about custom post types? I'd like to set my CPT to have sidebars left and right but can't find a way to do that.
... unless I'm missing something?
Hi there,
the Customizer settings apply to the Theme templates.
If you're not using a custom template for your CPT then the customizer settings, the sidebar settings under the layout meta in the post editor and the layout element will apply.
If you're using a custom template for your CPT then they would need to call the generate_construct_sidebars(); in your template. See here for where it is added in GPs single.php for example.
As a rule if you're wanting to create CPTs and maintain theme control - then using copies of the themes single and content templates is the best way to go.
Ah! Layout element I think is exactly what I'm after.
Cheers!
You're welcome
OK. I've had a good play around in Elements but it doesn't quite get me there. How can I make a 3 column layout on a CPT archive page? I'm playing around with the new dynamic content capabilities - trying to replicate the demo vid but hard to do with a CPT. CPTs don't pick up on the blog settings in Customiser and Elements don't give a column layout option for Archives.
I would have thought it would be quite easy to detect CPTs in Customiser and include a dropdown in the to apply settings? Or even have CPTs take on the blog settings as default? My Blog is set to 3 columns and CPT layout has 1.
Another thing - I found that disabling the title (in an Element) disables the single content heading but there isn't an option to disable the Archive heading... unless I'm missing something.
I tried this:
add_filter( 'generate_blog_columns','tu_portfolio_columns' );
function tu_portfolio_columns( $columns ) {
if ( is_post_type_archive( 'portfolio' ) ) {
return true;
}
return $columns;
}
But it just returned an error. Is this code deprecated?
https://docs.generatepress.com/article/using-columns-in-the-blog/#adding-columns-to-your-custom-post-type
That function should still apply - it shouldn't return an error unless you're already declaring the tu_portfolio_columns function elsewhere. If thats the case you need to use a different function name eg.
add_filter( 'generate_blog_columns','tu_portfolio_columns_for_cpt' );
function tu_portfolio_columns_for_cpt( $columns ) {
if ( is_post_type_archive( 'portfolio' ) ) {
return true;
}
return $columns;
}
Note: you must update the CPT slug ie 'portfolio' to match the CPT.
If that still isn't working then it will be because the CPT is using the themes archives ( not a custom archive template ) - for that you would use this snippet:
add_filter( 'generate_blog_columns', function( $columns ) {
if ( 'portfolio' === get_post_type() && ! is_singular() ) {
return true;
}
return $columns;
} );
Again - Note: you must update the CPT slug ie 'portfolio' to match the CPT.
Thanks David. The second snippet worked a treat.
I'm trying to avoid custom coding and just use the new tools where possible. The GP Elements system is very similar to Getwids Custom Posts block in that you can create block based templates and include dynamic Post content. Getwid can be used in the Editor area, Elements within block hooks. I can use the same CSS on both.
It would be great if Generateblocks could pull in Elements like Getwid does then I wouldn't need the extra plugin.
Glad to hear that.
We have a few ideas in mind regarding post loops and queries :) Thanks for sharing the info and the archive looks great!