Assign Sidebar To Custom Post Type

Hello:

I’m trying to assign a sidebar to a custom post type I created called Anemia, however, I am not seeing the post under All Pages and Posts on the Manage Sidebars screen. I wrote the following function cased on the article below, but it looks like it may be dated:

function add_sidebar_custom_post() {
if( get_post_type() == ‘anemia’) {
return ‘ups-sidebar-anemia-learn’;
}
}
add_filter( ‘ups-sidebar’, ‘add_sidebar_custom_post’ );

I’m still not seeing the posts in this screen and I’m not seeing a sidebar display.

I followed this thread here: https://theme.co/apex/forums/topic/assign-sidebar-to-custom-post-type/

Hi there,

Thanks for writing in.

That’s just assigning a sidebar in a sidebar area, what if your layout is not configured to have a sidebar (full-width). Please configure your layout at X > Launch > Options

Or, you can enforce it by adding this code to your child theme’s functions.php

  function x_get_content_layout() {

    $content_layout = x_get_option( 'x_layout_content' );

    if ( $content_layout != 'full-width' ) {
      if ( is_home() ) {
        $opt    = x_get_option( 'x_blog_layout' );
        $layout = ( $opt == 'sidebar' ) ? $content_layout : $opt;
      } elseif ( is_singular( 'post' ) ) {
        $meta   = get_post_meta( get_the_ID(), '_x_post_layout', true );
        $layout = ( $meta == 'on' ) ? 'full-width' : $content_layout;
      } elseif ( x_is_portfolio_item() ) {
        $layout = 'full-width';
      } elseif ( x_is_portfolio() ) {
        $meta   = get_post_meta( get_the_ID(), '_x_portfolio_layout', true );
        $layout = ( $meta == 'sidebar' ) ? $content_layout : $meta;
      } elseif ( is_page_template( 'template-layout-content-sidebar.php' ) ) {
        $layout = 'content-sidebar';
      } elseif ( is_page_template( 'template-layout-sidebar-content.php' ) ) {
        $layout = 'sidebar-content';
      } elseif ( is_page_template( 'template-layout-full-width.php' ) ) {
        $layout = 'full-width';
      } elseif ( is_archive() ) {
        if ( x_is_shop() || x_is_product_category() || x_is_product_tag() ) {
          $opt    = x_get_option( 'x_woocommerce_shop_layout_content' );
          $layout = ( $opt == 'sidebar' ) ? $content_layout : $opt;
        } else {
          $opt    = x_get_option( 'x_archive_layout' );
          $layout = ( $opt == 'sidebar' ) ? $content_layout : $opt;
        }
      } elseif ( x_is_product() ) {
        $layout = 'full-width';
      } elseif ( x_is_bbpress() ) {
        $opt    = x_get_option( 'x_bbpress_layout_content' );
        $layout = ( $opt == 'sidebar' ) ? $content_layout : $opt;
      } elseif ( x_is_buddypress() ) {
        $opt    = x_get_option( 'x_buddypress_layout_content' );
        $layout = ( $opt == 'sidebar' ) ? $content_layout : $opt;
      } elseif ( is_404() ) {
        $layout = 'full-width';
      } else {
        $layout = $content_layout;
      }
    } else {
      $layout = $content_layout;
    }
   
   if( get_post_type() == 'anemia') return 'content-sidebar';

    return $layout;

  }

Hope this helps.

Hi Rad, thanks for the reply. I wanted to clarify to others having the same issue that the code I pasted above does work. I was having a caching issue.

What did you mean by configure layout at X > Launch > Options? Do you mean Customizer > Layout and Design? Like where you would set the content layout?

If so, this brings up a good point with my custom post type. There’s currently no way of selecting different templates within the post itself. Once it’s set on the content layout in the customizer, I don’t have a way of changing it, so I don’t think the above will be needed unless I misunderstand. Thanks again for your help.

Hi,

Although you can also set the in the Customizer, we now recommend you do it in X Options.

Then select Options

and Select Layout and Design

Hope that helps.

Unfortunately, this doesn’t work. This forces the main sidebar on every page/post. I am able to see the custom sidebar on the pages and posts with my code below as I need them, but my code causes the main sidebar to disappear? Thoughts?

function add_sidebar_custom_post() {
if( get_post_type() == ‘anemia’ && in_category(‘anemia-lifestyle’) ) {
return ‘ups-sidebar-anemia-lifestyle’;
} elseif ( get_post_type() == ‘anemia’ && in_category(‘anemia-learn’) ) {
return ‘ups-sidebar-anemia-learn’;
} elseif( get_post_type() == ‘anemia’ && in_category(‘anemia-living’) ) {
return ‘ups-sidebar-anemia-living’;
}
}
add_filter( ‘ups_sidebar’, ‘add_sidebar_custom_post’ );

For what it’s worth, this seemed to fix the issue with the main sidebar disappearing:

function assign_custom_sidebar($sidebar){
if( get_post_type() == ‘anemia’ && in_category(‘anemia-lifestyle’) ) {
return ‘ups-sidebar-anemia-lifestyle’;
} elseif ( get_post_type() == ‘anemia’ && in_category(‘anemia-learn’) ) {
return ‘ups-sidebar-anemia-learn’;
} elseif( get_post_type() == ‘anemia’ && in_category(‘anemia-living’) ) {
return ‘ups-sidebar-anemia-living’;
}
return $sidebar;
}
add_filter( ‘ups_sidebar’, ‘assign_custom_sidebar’);

Hi There,

Glad this is now sorted out and thank you for sharing the fixed that work for you.

Thank you - your fix worked for me too. The standard fix supplied by X support removes the default sidebar from your other blog pages (if you have one set). This ensures that your custom posts get the custom sidebar and the rest of the blog keep the default sidebar.

function assign_custom_sidebar($sidebar){
if( get_post_type() == ‘yourcustompostslug’ ) {
return ‘ups-sidebar-the-latest’;
}
return $sidebar;
}
add_filter( ‘ups_sidebar’, ‘assign_custom_sidebar’);

Hi,

Thanks for sharing your code.

Have a nice day! :slight_smile: