- This topic has 8 replies, 3 voices, and was last updated 3 years, 10 months ago by
Tom.
-
AuthorPosts
-
September 29, 2021 at 1:34 pm #1947423
Josh
I’m attempting to create separate (unique) sidebar content for my custom post type single pages. All sidebars are currently off on the site (content-only).
I created a new side bar using this code:
//add new sidebar function rt_custom_sidebars() { register_sidebar(array( 'name' => 'Team Sidebar', 'id' => 'team-sidebar', 'description' => 'sidebar for team single pages', 'before_widget' => '<div class="teamwidget">', 'after_widget' => '</div>', 'before_title' => '<h3>', 'after_title' => '</h3>', ) ); } add_action( 'widgets_init', 'rt_custom_sidebars' );
This creates the widget fine for me.
I was hoping to assign it to single custom post type (‘pacific_inns_member”).
I tried with this code:
//choose sidebars for custom posts add_filter( 'generate_sidebar_layout', function( $layout ) { // If we are on a an event page if ( is_singular('pacific_inns_member') ) { return 'left-sidebar'; } // Or else, set the regular layout return $layout; } );
This successfully adds the left-sidebar. However if i change the line
return 'left-sidebar';
toreturn 'team-sidebar';
Nothing is displayed.What is the generatepress method for adding my new sidebar to only the single page of my custom post type?
(I am currently using a template “single-pacific_inns_member.php”. and tried replacing generate_construct_sidebars() but that eliminated formatting.)
Is there a filter to add an if statement to this function?September 29, 2021 at 8:04 pm #1947636Elvin
StaffCustomer Supporthi Josh,
I believe what you need to do is to modify what the
add_action( 'generate_sidebars', 'generate_construct_sidebars' );
https://github.com/tomusborne/generatepress/blob/b60b853630da6d9015722da903e53c8064148b0a/inc/structure/sidebars.php#L12-L43This is because it won’t matter if the filter returns
team-sidebar
if there’s no actualget_sidebar( 'team-sidebar' );
function to render the registered sidebar for you on the action hook for constructing the actual registered sidebar.I’d suggest removing the default
add_action( 'generate_sidebars', 'generate_construct_sidebars' );
by doing a
remove_action( 'generate_sidebars', 'generate_construct_sidebars' );
and then re-add the same function but modify it including yourget_sidebar( 'team-sidebar' );
condition. 😀September 30, 2021 at 10:15 am #1948467Josh
Thanks for the help with this Elvin. I believe I have it working, so I’m posting what I did in case anyone else needs it and to confirm with the GP team that this is in fact the way it should have been done.
1. I replaced the above code in my first post to make my sidebar with this code to address formatting issues (changed from DIV to ASIDE and added classes):
//add new sidebar(s) function rt_custom_sidebars() { register_sidebar(array( 'name' => 'Team Sidebar', 'id' => 'team-sidebar', 'description' => 'sidebar for team single pages', 'before_widget' => '<aside id="%1$s" class="widget inner-padding %2$s teamwidget ">', 'after_widget' => '</aside>', 'before_title' => '<h3>', 'after_title' => '</h3>', ) ); } add_action( 'widgets_init', 'rt_custom_sidebars' );
2. Used the code generate_sidebar code to assign the left-sidebar to my single post types as I don’t have a sidebar for the rest of the site.
3. I added this code to my functions.php
add_action( 'wp','rt_team_sidebar' ); function rt_team_sidebar() { if ( is_singular('pacific_inns_member') ) { remove_action( 'generate_sidebars','generate_construct_sidebars' ); add_action( 'generate_sidebars','rt_construct_sidebar' ); } } function rt_construct_sidebar() { get_sidebar( 'team' ); }
(note Elvin suggested using
get_sidebar( 'sidebar-team' )
but it should beget_sidebar( 'team' )
.)4. Next I copied the sidebar-left.php from my parent theme and put into my child theme. Renamed it to sidebar-team.php and changed this line:
if ( ! dynamic_sidebar( 'sidebar-2' ) ) { generate_do_default_sidebar_widgets( 'left-sidebar' ); }
to
if ( ! dynamic_sidebar( 'team-sidebar' ) ) { generate_do_default_sidebar_widgets( 'team-sidebar' ); }
5. Last, in my single-pacific_inns_member.php I changed this line:
generate_construct_sidebars();
to
rt_construct_sidebar();
This seems to be working. I’d really like a confirmation that it looks correct though. Specifically on the if statement in rt_team_sidebar().
September 30, 2021 at 7:49 pm #1948816Elvin
StaffCustomer SupportNice one! That’s good use of the hook.
Glad you got it sorted. Thanks for sharing it with us. 😀
October 1, 2021 at 4:56 pm #1949935Josh
So. Big question here, but I think this why I couldn’t wrap my head around this.
It says that the hook for
add_action( 'generate_sidebars','generate_construct_sidebars' );
was removed in 2.0. That’s why i had to edit the templates and couldn’t just leavegenerate_construct_sidebars()
there. Based on this knowledge there was a lot that I didn’t need here.The simplest thing to do would have been in my template file to just replace
generate_construct_sidebars()
withget_sidebar('team')
after having created the sidebar-team.php file.Wouldn’t that make more sense?
Or if I didn’t want to adjust template files we could maybe just rewrite the pluggable function
generate_construct_sidebars()
adding our if then statement there?It seems though that all this:
add_action( 'wp','rt_sidebars' ); function rt_sidebars() { if ( is_singular('pacific_inns_member') ) { remove_action( 'generate_sidebars','generate_construct_sidebars' ); add_action( 'generate_sidebars','rt_construct_team_sidebar' ); } if ( is_singular('pacific_inns_service') || is_archive ('pacific_inns_service') || is_page(8) ) { remove_action( 'generate_sidebars','generate_construct_sidebars' ); add_action( 'generate_sidebars','rt_construct_service_sidebar' ); } }
is actually not doing anything at all?
October 1, 2021 at 9:53 pm #1950026Josh
I tried using the pluggable function
generate_construct_sidebars()
as well. I addded this to my functions.phpfunction generate_construct_sidebars() { if ( is_singular('pacific_inns_member')) { get_sidebar( 'team' ); } elseif ( is_singular('pacific_inns_service') || is_archive ('pacific_inns_service') || is_page(8) ) { get_sidebar( 'service' ); } else { $layout = generate_get_layout(); // When to show the right sidebar. $rs = array( 'right-sidebar', 'both-sidebars', 'both-right', 'both-left' ); // When to show the left sidebar. $ls = array( 'left-sidebar', 'both-sidebars', 'both-right', 'both-left' ); // If left sidebar, show it. if ( in_array( $layout, $ls ) ) { get_sidebar( 'left' ); } // If right sidebar, show it. if ( in_array( $layout, $rs ) ) { get_sidebar(); } } } //choose sidebars for custom posts add_filter( 'generate_sidebar_layout', function( $layout ) { // If we are on a an member page if ( is_singular('pacific_inns_member') ) { return 'team'; } // If we are on service pages (id 8 is the main what we do page) if ( is_archive('pacific_inns_service') || is_singular('pacific_inns_service') || is_page(8) ) { return 'service'; } // Or else, set the regular layout return $layout; } );
That made all sorts of strange things mostly. only the service sidebar was showing up.
So I tried adding this:
function generate_construct_sidebars() { $layout = generate_get_layout(); // When to show the right sidebar. $rs = array( 'right-sidebar', 'both-sidebars', 'both-right', 'both-left' ); // When to show the left sidebar. $ls = array( 'left-sidebar', 'both-sidebars', 'both-right', 'both-left' ); // If left sidebar, show it. if ( in_array( $layout, $ls ) ) { get_sidebar( 'left' ); } // If right sidebar, show it. if ( in_array( $layout, $rs ) ) { get_sidebar(); } if ($layout=="team"){ get_sidebar('team'); } if ($layout=="service"){ get_sidebar('service'); } } //choose sidebars for custom posts add_filter( 'generate_sidebar_layout', function( $layout ) { // If we are on a an member page if ( is_singular('pacific_inns_member') ) { return 'team'; } // If we are on service pages (id 8 is the main what we do page) if ( is_archive('pacific_inns_service') || is_singular('pacific_inns_service') || is_page(8) ) { return 'service'; } // Or else, set the regular layout return $layout; } );
This works, but all sidebars (which were copied off sidebar-left) show up on the right side of the page.
I think i’m just confusting myself at this time…what am I doing wrong?!!?
October 2, 2021 at 8:06 pm #1950881Tom
Lead DeveloperLead DeveloperHi Josh,
What you said here is correct: https://generatepress.com/forums/topic/specifc-sidebars-for-custom-post-types/#post-1949935
Since you’re already using a child theme, you simply have to replace the
generate_construct_sidebars()
function call with yourget_sidebar( 'team' )
function. That’s it – nothing else should be required.Let me know 🙂
October 2, 2021 at 10:13 pm #1950915Josh
Thanks Tom. I tried a LOT of ways to make this work, but what you suggested is the only way that reliably worked. The theme is amazing, but it’s too bad this is the only way. Not faulting the theme though, I tried to figure out a method to make this work and couldn’t. It is too bad though that I have to import a template for each post type and archive to make this work. With 3 CPTs, this was 6 duplication of theme files to make it work.
October 3, 2021 at 7:51 pm #1951638Tom
Lead DeveloperLead DeveloperHave you considered a plugin like this?: https://wordpress.org/plugins/content-aware-sidebars/
Alternatively, you could use a Sidebar Block Element: https://docs.generatepress.com/article/block-element-overview/
-
AuthorPosts
- You must be logged in to reply to this topic.