- This topic has 5 replies, 3 voices, and was last updated 2 years, 4 months ago by Elvin.
-
AuthorPosts
-
November 23, 2021 at 11:49 pm #2019062Matthew
Try to add a custom sidebar to a CPT archive page. I’ve been scouring the documentation and forum but the only post I can find covering this topic is from 2015 and no longer works:
https://generatepress.com/forums/topic/how-do-i-add-a-custom-sidebar/I have already created a new widget position (cpt_sidebar) in functions.php and populated it. Now I just need to have it appear where the right sidebar currently does on the CPT archive only, not CPT posts or anywhere else.
add_action('generate_cpt_sidebar','generate_construct_sidebars'); function generate_construct_sidebars() { get_sidebar('cpt_sidebar'); }
Using this old code from 2015 with the current version of GP adds the existing right sidebar to normal pages that do not have a sidebar — even if the sidebar is specifically turned off in page settings — instead of the new cpt_sidebar widget position. Meanwhile the CPT archive remains unaffected; the existing right sidebar still shows despite
do_action('generate_cpt_sidebar');
being added to the archive-CPT.php template file.There has to be a simple way to do this but I haven’t been able to figure it out yet and could really use a bit of assistance. Any insight would be greatly appreciated, thanks!
November 24, 2021 at 3:39 am #2019319DavidStaffCustomer SupportHi there,
maybe this topic will help ?
https://generatepress.com/forums/topic/display-specific-widget-for-custom-post-type/#post-1158420
November 25, 2021 at 7:04 pm #2022545MatthewHey David,
Ahhh yes, this code looks familiar, thank you for the reminder. Believe I have used it on another site before. However, thus far I have been unable to customize the code correctly to get the new widget position (cpt_sidebar) to display on the CPT archive (new-templates) page. Am I missing something?
if ( is_singular( 'guides' ) ) { } elseif ( is_archive( 'new-templates' ) ) { if ( ! dynamic_sidebar( 'sidebar-1' ) ) { generate_do_default_sidebar_widgets( 'cpt_sidebar' ); } } else { if ( ! dynamic_sidebar( 'sidebar-1' ) ) { generate_do_default_sidebar_widgets( 'right-sidebar' ); } }
November 25, 2021 at 8:27 pm #2022602ElvinStaffCustomer SupportHi Matthew,
The code you shared is set to do nothing if the page is a single post page for the post type is
guides
as the if condition for it immediately closes w/o anything in it. Was that part intended?Plus, if you wish to set a condition to a specific CPT archive page, try using
is_post_type_archive()
https://developer.wordpress.org/reference/functions/is_post_type_archive/is_post_type_archive()
without args in it will return true for ALL CPT archive pages.is_post_type_archive()
with args, i.e.,is_post_type_archive('guides')
will return TRUE if the current page is an archive page for CPT guides.December 20, 2021 at 6:22 am #2054424MatthewHi Elvin,
Yes, that first part was intended. Another CPT customization ๐
As for the second part, you are right!
is_post_type_archive()
is what I should have been using instead ofis_archive()
thank you so much for the assistance! I was not even aware that existed. Cheers!December 20, 2021 at 8:08 pm #2055126ElvinStaffCustomer SupportNo problem. Happy holidays & Stay safe! ๐
-
AuthorPosts
- You must be logged in to reply to this topic.