• I’m trying to follow the instructions in [this “How to show different sidebars for posts and pages in WordPress” tutorial](https://getflywheel.com/layout/show-different-sidebars-posts-pages-wordpress-how-to/) to use different sidebars for different types of pages. In particular, I’m using a pretty simple child theme for the Woocommerce Storefront theme, and I want to use the regular sidebar for all shop pages that have products (so the default template), and I want to use my own sidebar (showing blog categories instead of product categories) for a “News” page that has my blog posts. I figured using the “Template-driven approach” in the tutorial would be my best option, but I’m definitely open to other ways. I’m not working on a live site (using Local software instead), so I don’t have a link.

    I registered my sidebar (as ‘blog-sidebar’) by adding this at the end of the functions.php in my child theme:

    
    function sf_child_theme_widgets_init() { 
    register_sidebar( 
    array( 
    'name'       => __( 'Blog Sidebar', 'sf_child_theme' ), 
    'id'         => 'blog-sidebar', 
    'description'   => __( 'Widgets in the blog sidebar', 'sf_child_theme' ), 
    'before_widget' => '<section id="%1$s" class="widget %2$s">', 
    'after_widget'  => '</section>', 
    'before_title'  => '<h2 class="widget-title">', 
    'after_title'   => '</h2>', 
    ) 
    ); 
    } 
    add_action( 'widgets_init', 'sf_child_theme_widgets_init' );
    

    The default sidebar comes from the parent theme.`

    Then I configured its widgets and created a blog-page.php with Template Name: News & Events, and edited my page to use that template. At the end of my custom template, just before the footer, I included get_sidebar('blog-sidebar'); The rest of that template comes from page.php in the parent theme, so now I have this:

    
    <?php
    /**
     * Template Name: News & Events
     *
     * The template for displaying blog pages.
     *
     *
     * @package storefront-child
     */
    get_header(); ?>
    
    	<div id="primary" class="content-area">
    		<main id="main" class="site-main" role="main">
    
    			<?php
    			while ( have_posts() ) :
    				the_post();
    
    				do_action( 'storefront_page_before' );
    
    				get_template_part( 'content', 'page' );
    
    				/**
    				 * Functions hooked in to storefront_page_after action
    				 *
    				 * @hooked storefront_display_comments - 10
    				 */
    				do_action( 'storefront_page_after' );
    
    			endwhile; // End of the loop.
    			?>
    
    		</main><!-- #main -->
    	</div><!-- #primary -->
    
    <?php
    get_sidebar('blog-sidebar');
    
    get_footer();
    ?>
    
    

    Then I copied sidebar.php into my child theme folder and started by replacing ‘sidebar-1’ with ‘blog-sidebar’ like this:

    
    if ( ! is_active_sidebar( 'blog-sidebar' ) ) {
    	return;
    }
    ?>
    
    <div id="secondary" class="widget-area" role="complementary">
    	<?php dynamic_sidebar( 'blog-sidebar' ); ?>
    </div><!-- #secondary -->
    

    This makes my custom sidebar show up correctly, but of course it’s also replacing the Woocommerce sidebar on the shop pages (which I’m guessing is ‘sidebar-1’). So I tried this, adapting the instructions in the tutorial:

    
    if ( (is_page_template('blog-page.php') ) && ( is_active_sidebar( 'blog-sidebar' ))) {    
        ?>
           <div id="secondary" class="widget-area" role="complementary">
        	<?php dynamic_sidebar( 'blog-sidebar' ); ?>
           </div><!-- #secondary -->    
        <?php
    }
    elseif ( is_active_sidebar( 'sidebar-1' )) {
            ?>    
           <div id="secondary" class="widget-area" role="complementary">
        	<?php dynamic_sidebar( 'sidebar-1' ); ?>
           </div><!-- #secondary -->    
        <?php
        }
    

    That just shows the default shop sidebar on every page though, so I guess my is_page_template('blog-page.php') isn’t working. I’ve researched this for days now and couldn’t find a solution that applied to what I’m trying to do (or maybe I just didn’t know how to apply it correctly – still learning!). I would really, really appreciate it if you could help me figure this out!

    Everything is updated, and I’ve tried disabling all plugins besides Woocommerce, but that doesn’t change anything. Here is the status report:

    
    ### WordPress Environment ###
    
    WordPress address (URL): https://dbibclone.local
    Site address (URL): https://dbibclone.local
    WC Version: 5.1.0
    REST API Version: ? 5.1.0
    WC Blocks Version: ? 4.4.3
    Action Scheduler Version: ? 3.1.6
    WC Admin Version: ? 2.0.2
    Log Directory Writable: ?
    WP Version: 5.7
    WP Multisite: –
    WP Memory Limit: 256 MB
    WP Debug Mode: –
    WP Cron: ?
    Language: en_US
    External object cache: –
    
    ### Server Environment ###
    
    Server Info: nginx/1.16.0
    PHP Version: 7.4.1
    PHP Post Max Size: 1,000 MB
    PHP Time Limit: 1200
    PHP Max Input Vars: 4000
    cURL Version: 7.67.0
    OpenSSL/1.1.1d
    
    SUHOSIN Installed: –
    MySQL Version: 8.0.16
    Max Upload Size: 300 MB
    Default Timezone is UTC: ?
    fsockopen/cURL: ?
    SoapClient: ?
    DOMDocument: ?
    GZip: ?
    Multibyte String: ?
    Remote Post: ?
    Remote Get: ?
    
    ### Database ###
    
    WC Database Version: 5.0.0
    WC Database Prefix: wp_
    Total Database Size: 9.66MB
    Database Data Size: 6.27MB
    Database Index Size: 3.39MB
    wp_woocommerce_sessions: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
    wp_woocommerce_api_keys: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    wp_woocommerce_attribute_taxonomies: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
    wp_woocommerce_downloadable_product_permissions: Data: 0.02MB + Index: 0.06MB + Engine InnoDB
    wp_woocommerce_order_items: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
    wp_woocommerce_order_itemmeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    wp_woocommerce_tax_rates: Data: 0.02MB + Index: 0.06MB + Engine InnoDB
    wp_woocommerce_tax_rate_locations: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    wp_woocommerce_shipping_zones: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
    wp_woocommerce_shipping_zone_locations: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    wp_woocommerce_shipping_zone_methods: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
    wp_woocommerce_payment_tokens: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
    wp_woocommerce_payment_tokenmeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    wp_woocommerce_log: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
    wp_actionscheduler_actions: Data: 0.02MB + Index: 0.11MB + Engine InnoDB
    wp_actionscheduler_claims: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
    wp_actionscheduler_groups: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
    wp_actionscheduler_logs: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    wp_commentmeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    wp_comments: Data: 0.02MB + Index: 0.09MB + Engine InnoDB
    wp_links: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
    wp_mailchimp_carts: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
    wp_mailchimp_jobs: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
    wp_options: Data: 2.44MB + Index: 0.06MB + Engine InnoDB
    wp_postmeta: Data: 2.52MB + Index: 1.89MB + Engine InnoDB
    wp_posts: Data: 0.36MB + Index: 0.20MB + Engine InnoDB
    wp_sbi_instagram_feeds_posts: Data: 0.00MB + Index: 0.00MB + Engine MyISAM
    wp_sbi_instagram_posts: Data: 0.10MB + Index: 0.00MB + Engine MyISAM
    wp_term_relationships: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
    wp_term_taxonomy: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    wp_termmeta: Data: 0.00MB + Index: 0.01MB + Engine MyISAM
    wp_terms: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    wp_usermeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    wp_users: Data: 0.02MB + Index: 0.05MB + Engine InnoDB
    wp_wc_admin_note_actions: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
    wp_wc_admin_notes: Data: 0.05MB + Index: 0.00MB + Engine InnoDB
    wp_wc_category_lookup: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
    wp_wc_customer_lookup: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    wp_wc_download_log: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    wp_wc_order_coupon_lookup: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    wp_wc_order_product_lookup: Data: 0.02MB + Index: 0.06MB + Engine InnoDB
    wp_wc_order_stats: Data: 0.02MB + Index: 0.05MB + Engine InnoDB
    wp_wc_order_tax_lookup: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    wp_wc_product_meta_lookup: Data: 0.02MB + Index: 0.09MB + Engine InnoDB
    wp_wc_reserved_stock: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
    wp_wc_tax_rate_classes: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
    wp_wc_webhooks: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
    
    ### Post Type Counts ###
    
    amn_mi-lite: 1
    attachment: 66
    customize_changeset: 5
    jetpack_migration: 2
    jp_img_sitemap: 1
    jp_sitemap: 1
    jp_sitemap_master: 1
    nav_menu_item: 24
    page: 19
    post: 18
    product: 14
    product_variation: 28
    revision: 55
    tribe_events: 7
    tribe_venue: 3
    wpsc_cart_orders: 538
    
    ### Security ###
    
    Secure connection (HTTPS): ?
    					Your store is not using HTTPS. Learn more about HTTPS and SSL Certificates.
    Hide errors from visitors: ?
    
    ### Active Plugins (5) ###
    
    Mailchimp for WooCommerce: by Mailchimp – 2.5.1
    The Events Calendar: by The Events Calendar – 5.5.0.1
    WooCommerce Shipping & Tax: by Automattic – 1.25.10
    WooCommerce: by Automattic – 5.1.0
    WP Super Cache: by Automattic – 1.7.2
    
    ### Inactive Plugins (10) ###
    
    Akismet Anti-Spam: by Automattic – 4.1.9
    All-in-One WP Migration: by ServMask – 7.40
    Contact Page: by Marek Bosman – 1.0
    Facebook for WooCommerce: by Facebook – 2.3.5
    Google Ads & Marketing by Kliken: by Kliken – 1.0.7
    Google Analytics for WordPress by MonsterInsights: by MonsterInsights – 7.17.0
    Hello Dolly: by Matt Mullenweg – 1.7.2
    Jetpack by WordPress.com: by Automattic – 9.5
    MOJO Marketplace: by Mike Hansen – 0.5.9
    Smash Balloon Instagram Feed: by Smash Balloon – 2.9
    
    ### Settings ###
    
    API Enabled: –
    Force SSL: –
    Currency: USD ($)
    Currency Position: left
    Thousand Separator: ,
    Decimal Separator: .
    Number of Decimals: 2
    Taxonomies: Product Types: external (external)
    grouped (grouped)
    simple (simple)
    variable (variable)
    
    Taxonomies: Product Visibility: exclude-from-catalog (exclude-from-catalog)
    exclude-from-search (exclude-from-search)
    featured (featured)
    outofstock (outofstock)
    rated-1 (rated-1)
    rated-2 (rated-2)
    rated-3 (rated-3)
    rated-4 (rated-4)
    rated-5 (rated-5)
    
    Connected to WooCommerce.com: –
    
    ### WC Pages ###
    
    Shop base: #1956 - /shop/
    Cart: #1957 - /cart/
    Checkout: #1958 - /checkout/
    My account: #1959 - /my-account/
    Terms and conditions: ? Page not set
    
    ### Theme ###
    
    Name: Storefront Child
    Version: 1.0
    Author URL: 
    Child Theme: ?
    Parent Theme Name: Storefront
    Parent Theme Version: 3.5.1
    Parent Theme Author URL: https://woocommerce.com/
    WooCommerce Support: ?
    
    ### Templates ###
    
    Overrides: –
    
    ### Action Scheduler ###
    
    Complete: 35
    Oldest: 2021-04-03 03:24:23 +0000
    Newest: 2021-04-05 09:00:40 +0000
    
    ### Status report information ###
    
    Generated at: 2021-04-06 03:13:43 +00:00
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    Hi there ??

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Cheers!

    Thread Starter amillionmiles

    (@amillionmiles)

    Thank you! I didn’t realize it was so complex – I thought it was just a matter of adding a small if-clause, or adding another sidebar file, or something like that. I was just wondering where I got my formula wrong. I posted the same question on StackExchange and received a reply for something that worked for a theme that doesn’t have Woocommerce/Storefront, so I’m just wondering what I overlooked.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘sidebar depending on custom template’ is closed to new replies.