Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Widget area in my Wordpress Timber Theme

I am using Timber to create a very basic Wordpress theme.

I'm stuck though.

I want to create Widget areas in the theme and I can't figure it out.

I've tried following the instructions, for creating a Dynamic Widget, on this page - https://github.com/jarednova/timber/wiki/Sidebar#method-3-dynamic

By following this I've added the following code to my files - To page-frontpage.php I added

$context['dynamic_sidebar'] = Timber::get_widgets('dynamic_sidebar');

so it now looks like this:

<?php

$context = Timber::get_context();
$post = new TimberPost();
$context['post'] = $post;
$context['dynamic_sidebar'] = Timber::get_widgets('dynamic_sidebar');
Timber::render(array('page-' . $post->post_name . '.twig', 'page.twig'), $context);

I then added this to the base.twig file

<section>
    {{ dynamic_sidebar }}
</section>

I expected to see a new Widget area in the Wordpress Admin under Appearance -> Customize but got nothing.

I've struggled with this for the past few hours and made little progress, any help would be greatly appreciated.

Thank you.

like image 692
John Behan Avatar asked Dec 02 '22 18:12

John Behan


1 Answers

I've fixed this issue.

I just had to go back to basics and create the widgets in the functions.php file, so now my code looks something like this.

in functions.php

register_sidebar( array(
            'name' => 'Home left sidebar',
            'id' => 'home_left',
            'before_widget' => '<div>',
            'after_widget' => '</div>',
            'before_title' => '<h2 class="rounded">',
            'after_title' => '</h2>',
        ) );

in page-home.php

$context['home_left'] = Timber::get_widgets('home_left');

in home.twig

<div class="left-box">{{ home_left }}</div>

Hope this helps someone in the future

like image 100
John Behan Avatar answered Dec 22 '22 20:12

John Behan