Site logo

Archived topic

Adding a custom sidebar to CPT (custom post type) archive

5 replies · Started by Matthew on November 23, 2021

Viewing posts 1–6 of 6

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!

Hey 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' );
			}
		}

Hi 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.

Hi 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 of is_archive() thank you so much for the assistance! I was not even aware that existed. Cheers!

No problem. Happy holidays & Stay safe! :D

This archived topic is closed to new replies.