- This topic has 12 replies, 2 voices, and was last updated 7 years, 2 months ago by Tom.
-
AuthorPosts
-
January 15, 2017 at 2:44 am #264211Daniel
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.
January 15, 2017 at 9:21 am #264292TomLead DeveloperLead DeveloperYou 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/January 15, 2017 at 1:56 pm #264382DanielThanks 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?
January 15, 2017 at 9:30 pm #264436TomLead DeveloperLead DeveloperIn your custom sidebar, you should be able to click “Sidebar Locations” and set it as the sidebar for your specific post type.
January 20, 2017 at 5:23 am #266151DanielBrilliant! I will try that.
January 21, 2017 at 2:05 am #266464DanielHey 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.January 21, 2017 at 2:31 am #266480DanielAlso, 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;
}January 21, 2017 at 10:55 am #266603TomLead DeveloperLead DeveloperWhen 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.
January 21, 2017 at 11:02 am #266606DanielHi 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
January 21, 2017 at 11:08 am #266607TomLead DeveloperLead DeveloperHmm, 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?
January 21, 2017 at 11:51 am #266620DanielReally 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/January 21, 2017 at 12:28 pm #266634DanielThe 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.January 21, 2017 at 8:18 pm #266717TomLead DeveloperLead DeveloperYou’ll need to check with them to see if they plan on supporting custom taxonomies or if it’s already possible.
-
AuthorPosts
- You must be logged in to reply to this topic.