Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Problem getting widgets to show up in template on WordPress

Hello

I've been following the videos in the Bootstrap to WordPress course and have attempted to add in some widget areas on my template. Here's the code in my functions.php file:

function create_widget($name, $sid, $description) {

    register_sidebar(array(
        'name' => __( $name ),
        'id' => $id,
        'before_widget' => '<div class="widget">',
        'after_widget' => '</div>',
        'before_title' => '<h3>',
        'after_title' => '</h3>'
    ));

}

create_widget( 'Front Page Left', 'front-left', 'Displays on the left of the homepage' );
create_widget( 'Front Page Centre', 'front-centre', 'Displays in the centre of the homepage' );
create_widget( 'Front Page Right', 'front-right', 'Displays on the right of the homepage' );
create_widget( 'Page Sidebar', 'page', 'Displays on the side of pages with a sidebar' );

I have this code on the sidebar.php file:

<div class="col-md-3 sidebar">

    <?php if ( ! dynamic_sidebar( 'page' ) ): ?>

    <h3>Sidebar Setup</h3>
    <p>Please add widgets to the page sidebar to have them display here.</p>
    <p><a class="btn btn-default" href="#" role="button">View details &raquo;</a></p>

    <?php endif; ?>

</div>

On my Widgets page in the Admin Area I only see "Page Sidebar" and when I attempt to add anything into it, like some basic text, it keeps disappearing and won't display.

When I view the page from the front end, I get the "Please add widgets to the page sidebar to have them display here" paragraph.

Firstly, why aren't my other 3 widgets appearing in the admin area and secondly, why is nothing saving nor being displayed on the front end?

As far as I can see, everything is published as it should be.

Suggestions welcome.

Hello James Pask,

First you have a php error in your function

On your function above you have

function create_widget($name, $sid, $description){

}

You added $sid instead of $id. Try to change that and all widget will appear in the admin area.

If you want to display your widgets on the different location example: on the front page and template page etc.

You have to create a new sidebar.php file and append the name of your widget like so sidebar-page.php or sidebar-front-left.php and then call it using <?php get_sidebar('page'); ?> <?php get_sidebar('front-left'); ?>

Hope this helps.

1 Answer

Thanks Gerardo, that fixed it and the content even saves in the widget area!

Must have been my dodgy inputting! Thanks for your help, really appreciate it.