[Support request] Sidebar for Custom Post Types

Home Forums Support [Support request] Sidebar for Custom Post Types

Home Forums Support Sidebar for Custom Post Types

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #264211
    Daniel

    How do I create a sidebar for a custom post type single page?
    I’m using WPSP to loop out a custom post type listing page.
    When you click the read more button on the listing page it takes you through to a single detail page for that custom post type, which in this case is called horses.
    However, the default right sidebar is still being pulled in.
    Would be good if I could change this to the left sidebar or a new custom sidebar.

    I looked at the GP documentation regarding sidebars but I could not find anything specific to custom post types.
    On this page https://docs.generatepress.com/article/sidebar-layout/ I took the WooCommerce example and modified it in the hope that it would work, but it didn’t.
    Would be great if someone could help on this.

    My CPT is horses and my single page is single-horses.php

    The code I added to my child themes function page is:

    add_filter( 'generate_sidebar_layout','cpt_horses_sidebar_layout' );
    function cpt_horses_sidebar_layout( $layout )
    {
    	// If we are on the horses single page, set the sidebar
    	if ( function_exists( is_singular( 'horses' ) ) )
    		return 'left-sidebar';
    
    	// Or else, set the regular layout
    	return $layout;
    
    }

    My PHP skills are limited, so forgive me if I have a made a mistake with the code.

    Thanks in advance.

    #264292
    Tom
    Lead Developer
    Lead Developer

    You were very close!

    This would be it:

    add_filter( 'generate_sidebar_layout','cpt_horses_sidebar_layout' );
    function cpt_horses_sidebar_layout( $layout )
    {
    	// If we are on the horses single page, set the sidebar
    	if ( is_singular( 'horses' ) )
    		return 'left-sidebar';
    
    	// Or else, set the regular layout
    	return $layout;
    
    }

    To set custom sidebar widgets for individual pages/post types etc.. using a plugin is the best method. There’s a couple options to choose from:
    https://en-ca.wordpress.org/plugins/custom-sidebars/
    https://en-ca.wordpress.org/plugins/display-widgets/

    #264382
    Daniel

    Thanks Tom!

    I am already using the custom sidebars plugin. Will that let me return ‘horse-sidebar’; in the php code, or do I have to select sidebars manually in the CMS?

    #264436
    Tom
    Lead Developer
    Lead Developer

    In your custom sidebar, you should be able to click “Sidebar Locations” and set it as the sidebar for your specific post type.

    #266151
    Daniel

    Brilliant! I will try that.

    #266464
    Daniel

    Hey Tom,

    Been using Custom Sidebars, it’s great, but I am having an issue with it which is holding back the completion of a site. This is not specifically GP related, more Custom Sidebars, but as you use it, you may be able to help.
    I have a custom post type called “siteresources” which is being looped out on a page called downloads using WPSP, there is a custom sidebar on the page which shows categories for that custom post type.
    My problem is, when I click a category on the right, the custom sidebar reverts back to the standard blog sidebar.
    So close to finishing this site, any help would be appreciated.

    http://staging.postnatalexercise.co.uk/downloads/

    #266480
    Daniel

    Also, I am using the function below to try and make that post type enable masonry, but it does not seem to work either.

    function tu_portfolio_masonry( $masonry )
    {
    if ( is_post_type_archive( ‘siteresources’ ) ) :
    return ‘true’;
    endif;

    return $masonry;
    }

    #266603
    Tom
    Lead Developer
    Lead Developer

    When you click on a category, it looks like it’s taking you to a custom taxonomy page, so you’ll need to set the sidebar to show up on that taxonomy as well.

    That code will only work on the archives page of that post type. If you’re using WPSP you can use the masonry option built into it.

    #266606
    Daniel

    Hi Tom,

    I’m only using WPSP for the listing page, when it goes through to archive page is when I want masonry applied. I have tried to add your function, but does not seem to work for me.

    Also I have tried to set the sidebar to show on the custom taxonomy, but in Custom Sidebars that taxonomy is not showing up. Maybe it is a limitation using the free version?

    I might have to use another plugin, which will be a extra lot of work.
    So close to finishing this site too.

    Cheers

    #266607
    Tom
    Lead Developer
    Lead Developer

    Hmm, you might need to open a support topic to see if they support custom taxonomies.

    Display Widgets is another plugin you can try.

    Can you link me to the archives page of the post type?

    #266620
    Daniel

    Really frustrating, nearly finished this site and such a small thing is holding me back.
    Joys of development 🙂

    This is the main page with WPSP on http://staging.postnatalexercise.co.uk/downloads/
    The custom sidebar is showing fine, when you click read more it’s fine too.
    But when you click on the sidebar taxonomies it does show the correct content on the page, but changes the sidebar back to the default blog one.
    I’m using Custom Post Types UI to create taxonomies and post types, but I don’t think that is causing an issue.
    Be a shame to have to use another plugin, as it works so well everywhere else.

    Below is the Archives page.
    http://staging.postnatalexercise.co.uk/resourcecategory/marketing/

    #266634
    Daniel

    The Display Widgets plugin works (thanks for that).
    Would rather get it working with Custom Sidebars to avoid plugin bloat, but I guess I will just have to go with what works.

    #266717
    Tom
    Lead Developer
    Lead Developer

    You’ll need to check with them to see if they plan on supporting custom taxonomies or if it’s already possible.

Viewing 13 posts - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.