Archived topic
Different layouts for different custom post types
5 replies · Started by Dhruv on July 17, 2016
Hello Sir
I have 2 custom post types (Notes and Downloads) in my website.
How can I set different layouts for its single.php posts.
In notes post type I want layout like
Sidebar/content
While in downloads post type I want layout like
content/sidebar
Kindly suggest me any code for getting this functionality.
Thanks
Hi there,
You can use our sidebar filter:
add_filter( 'generate_sidebar_layout','generate_custom_sidebar_layout' );
function generate_custom_sidebar_layout( $layout )
{
if ( is_post_type_archive( 'downloads' ) )
return 'right-sidebar';
if ( is_post_type_archive( 'notes' ) )
return 'left-sidebar';
// Or else, set the regular layout
return $layout;
}
Adding PHP: http://generatepress.com/knowledgebase/adding-php-functions/
Hi..
Thank you for support.
I have copied the code in functions.php
But it is not working. It is not changing default layout set in customiser.
Regards
That code won't do anything to the Customizer, it will just change the sidebar layout on the "notes" and "downloads" post type archives.
Make sure the post types are named "notes" and "downloads" specifically - if they're different then those names need to change.
Hello Tom,
Actually I wanted the sidebars on singular pages of custom post types.
So, I just changes the code from is_post_type_archives() to is_singular() and it is working now.
add_filter( 'generate_sidebar_layout','generate_custom_sidebar_layout' );
function generate_custom_sidebar_layout( $layout )
{
if ( is_singular( $post_types = 'download' ) )
return 'right-sidebar';
if ( is_singular( $post_types = 'notes' ) )
return 'left-sidebar';
// Or else, set the regular layout
return $layout;
}
Thanks for Guiding...This issue is resolved :)
Glad I could help :)
