• I tried adding widgets to the Blog Sidebar, but the sidebar isn’t showing on my pages, which makes sense, since the text under “Blog Sidebar” says, “Add widgets here to appear in your sidebar on blog posts and archive pages” – none of which I have on my site.

    Is there a way to add a sidebar to all of the pages on my site?

Viewing 1 replies (of 1 total)
  • in your child theme, create a file with the file name sidebar-page.php, containing this code:

    <?php
    /**
     Template Name: Sidebar Page
     */
    
    get_header(); ?>
    
    <div class="wrap">
    	<div id="primary" class="content-area">
    		<main id="main" class="site-main" role="main">
    
    			<?php
    			while ( have_posts() ) : the_post();
    
    				get_template_part( 'template-parts/page/content', 'page' );
    
    				// If comments are open or we have at least one comment, load up the comment template.
    				if ( comments_open() || get_comments_number() ) :
    					comments_template();
    				endif;
    
    			endwhile; // End of the loop.
    			?>
    
    		</main><!-- #main -->
    	</div><!-- #primary -->
    	<?php get_sidebar(); ?>
    </div><!-- .wrap -->
    
    <?php get_footer();

    add this to functions.php or your child theme:

    add_filter( 'body_class', 'sidebar_page_body_classes', 12 );
    
    function sidebar_page_body_classes( $classes ) {
        if ( is_page_template( 'template-parts/sidebar-page.php' ) && !is_front_page() ) {
    		if( in_array('page-two-column', $classes) ) {
            	unset( $classes[array_search('page-two-column', $classes)] );
    			$classes[] = 'page-one-column'; 
    		}
    		$classes[] = 'has-sidebar'; 
        }
    	return $classes;
    }

    https://developer.wordpress.org/themes/advanced-topics/child-themes/
    https://developer.wordpress.org/themes/template-files-section/page-template-files/
    https://developer.wordpress.org/reference/functions/body_class/

Viewing 1 replies (of 1 total)

The topic ‘Add sidebar to static pages?’ is closed to new replies.