• Heya,

    What is the simplest way to display a widget area? I feel like the documentation and verbiage regarding this is obnoxiously complicated and unhelpful.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Do you mean, how to add widgets to your theme or how to add the widgets to the area to your website.

    I’ll assume you mean to your theme.

    The easiest way is to insert this snippet in your functions.php

    function blm_register_sidebars() {
    	register_sidebar(
    		array(
    			'id' => 'primary',
    			'name' => __( 'Primary Sidebar', 'blm_basic' ),
    			'description' => __( 'The following widgets will appear in the main sidebar div.', 'blm_basic' ),
    			'before_widget' => '<aside id="%1$s" class="widget %2$s">',
    			'after_widget' => '</aside>',
    			'before_title' => '<h4>',
    			'after_title' => '</h4>'
    		)
    	);
    }

    Note the blm_prefix — I use that for my theme, you should have your own unique one…

    Then in the sidebar.php, you want to use something like:

    <?php if ( ! dynamic_sidebar( 'primary' ) ) : ?>
    
        <aside id="categories" class="widget"><h4>Categories</h4>
    		<ul>
    		<?php wp_list_categories( 'title_li=' ); ?>
    		</ul>
        </aside>
    
    <?php endif; ?>

    So if your theme doesn’t have any widgets installed your sidebar will display the categories. You can of course display anything else you want.

    If you’re building sites for clients I would look at the twenten theme. They use a lot of widgets. Automattic’s _s is a really good theme to look at too.

    Thread Starter Spencer Hill

    (@s3w47m88)

    Thank’s Christina,

    I find that there is a lot of similar lingo so it’s frustratingly difficult to describe this in a way everyone seems to understand (not your fault of course!).

    I have a widget area already defined in functions.php that appears in Appearance -> Widgets. I dragged a widget into that widget area and saved it. Then I went to my theme files and attempted to place the code (I believed to be what was needed) into the theme so that area, and the widget within it, would appear.

    But it’s not working so I thought maybe I had my syntax wrong or something, hence this post.

    My theme is a child theme of Twenty Eleven and it’s using the Main Sidebar widget area. Here is the exact code I’m using:

    <?php dynamic_sidebar( 'sidebar-1' ); ?>
    
    <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar("sidebar-1") ) : ?>
    <?php endif; ?>
    
    <?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar("sidebar-1") ) : ?>
    <?php endif; ?>

    I understand that these probably don’t all work, I was just trying different methods to try and see if I was overlooking something in the syntax.

    Also, can you explain how <?php if ( ! dynamic_sidebar( ‘primary’ ) ) : ?> would display my widget area? That seems like just a conditional statement to me, not code that would actually display the specified widgets contained within the specified widget area.

    The code ‘<?php if ( ! dynamic_sidebar( ‘primary’ ) ) : ?>’ means..

    If there is NO sidebar, then do something else.

    The ! symbol means NOT….

    The code ‘<?php if ( ! dynamic_sidebar( ‘primary’ ) ) : ?>’ means..

    If there is NO sidebar, then do something else.

    The ! symbol means NOT….

    I think that the simplest way to display the widget would be like so:

    <div id="sidebar">
    
    	<?php dynamic_sidebar( 'sidebar-1' ); ?>
    
    </div>

    here’s a good tutorial about widgets and sidebars – https://justintadlock.com/archives/2010/11/08/sidebars-in-wordpress

    just looking at your code again, make sure that in your function, when you register the sidebar, the id is sidebar-1

    i.e
    'id' => 'sidebar-1',

    Hi I answered this a couple of days ago someone wanted a horizontal widget area, there is an example with a twenty eleven child theme step by step workflow, this may help!

    Uses condition is_active_sidebar( 'horizontal-1' ) instead of the php !, and will output the div within the condition, so if the ‘horizontal-1’ widget area is empty it will not display!

    NOTE ON SIDEBAR-1:
    The parent twenty eleven already adds the sidebars sidebar-1 to sidebar-5, so you should avoid using sidebar-1 as that id already exists!

    HTH

    David

    Thread Starter Spencer Hill

    (@s3w47m88)

    Thanks for your response Christine! I actually do understand PHP and that syntax, I guess I just don’t understand how a conditional statement would display the widget itself without declaring somewhere in that code “Hey! Put the widget area here!”

    Does that make better sense?

    Thanks DR! I’ll give that a shot.

    All in all, my code above was working but it turned out that I had WP Super Cache installed and it wasn’t reflecting my changes! So once I set it to not cache for signed in users it work!

    declaring somewhere in that code “Hey! Put the widget area here!”

    This call is a WordPress PHP function that does just that, it will return the contents of the widget area,

    dynamic_sidebar( 'sidebar-1' );

    Also again ‘sidebar-1’ has already been created in the twenty eleven parent theme, so your sidebar name should be changed!

    Twenty Eleven – functions.php has:

    register_sidebar( array(
    	'name' => __( 'Main Sidebar', 'twentyeleven' ),
    	'id' => 'sidebar-1',
    	'before_widget' => '<aside id="%1$s" class="widget %2$s">',
    	'after_widget' => "</aside>",
    	'before_title' => '<h3 class="widget-title">',
    	'after_title' => '</h3>',
    ) );

    HTH

    David

    Thread Starter Spencer Hill

    (@s3w47m88)

    Oh sorry, I was responding to Christine’s code, you’re made perfect sense! ??

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Simplest way to display a widget area, need help…’ is closed to new replies.