Display Sidebar Widget in a Template


#1

Hello,
I’ve populated the Sidebar Widget (under Widgets) and it appears on pages with the Default Template only - but under the content, not on the side.

https://snag.gy/HlQ54R.jpg

Is there a way to modify the default template or create a template that will be full width, but include the sidebar widget?

Thank you.

Andy


#2

Hello Andy,

To edit the template’s file, you have to use a child theme to avoid future update issue. Because all files in the main theme are wiped out and replaced with new ones. Duplicate page-templates/template_fullwidth.php from main theme to your child theme folder. Add this snippet right after the loop.

<?php 
  if ( is_active_sidebar( 'your-sidebar-slug' ) ) {
    dynamic_sidebar( 'your-sidebar-slug' ); 
  }  
?>

Replace your-sidebar-slug with the sidebar slug name, which you’d like to display.

Your complete code you’d look like this:

<?php
/*
Template Name: Fullwidth
*/

get_header(); ?>
<div class="row">
	<div id="primary" class="content-area fullwidth">
		<main id="main" class="site-main" role="main">

			<?php
			while ( have_posts() ) : the_post();

				get_template_part( 'template-parts/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.
			?>

			<?php 
			  if ( is_active_sidebar( 'your-sidebar-slug' ) ) {
			    dynamic_sidebar( 'your-sidebar-slug' ); 
			  }  
			?>

		</main><!-- #main -->
	</div><!-- #primary -->

</div>
<?php
get_footer();

Other easier solution might be use the Widget Areas plugin. To display widgets within page content area, you can use the provided shortcode and insert it into page editor box.

Regards,
Kharis


#3

Unfortunately, this is not working for me.

I’ve created a new page in the Child Theme called template_fullwidth.php, and added the code you provided above, but I don’t see that it is making any changes to my site.

Firstly, I don’t see the Sidebar Widget as something that I can choose and further, if I did, I would know what the code was to put in place of ‘your-sidebar-slug’

In the meantime, I’ve just created full width pages with two columns (one 75% one 25%) and put a HTML Widget with twitter feed code in there followed by the Search Widget.

Not the easiest way to do this - especially if I want to change or add something.

The other thing is, my site is being developed locally, so I may have to wait to make these types of changes until the site goes live or is at least in development stage and hosted on the Internet.


#4

Hello there,

Have you activated the child theme?

Regards,
Kharis


#5

Yes, the Child Theme for Talon is active and running.


#6

Hello there,

If you have site caching with a plugin like WP Super Cache, you’ve to flush it when new code addition. Because it usually blocks the code to take effect in the front end. https://codex.wordpress.org/I_Make_Changes_and_Nothing_Happens

Regards,
Kharis