1

I want to create a new sidebar widget area in the header of my child theme. What would be the best way to do this?

Here is the code for one of the sidebars in the parent theme:

<?php
add_action( 'widgets_init', 'ci_widgets_init' );
if ( ! function_exists( 'ci_widgets_init' ) ) :
    function ci_widgets_init() {
        register_sidebar( array(
            'name'          => __( 'Blog Sidebar', 'ci_theme'),
            'id'            => 'blog-sidebar',
            'description'   => __( 'The list of widgets assigned here will appear in your blog posts.', 'ci_theme'),
            'before_widget' => '<aside id="%1$s" class="widget %2$s group">',
            'after_widget'  => '</aside>',
            'before_title'  => '<h3 class="widget-title"><span>',
            'after_title'   => '</span></h3>',
        ) );
    }

Can I just change the name, id and description, and put it in my child theme's functions.php file?

3

Well, this is what worked for me. In my functions.php file I put the following code:

function header_widgets_init() {

register_sidebar( array(

    'name' => 'Header Sidebar',

    'id' => 'header_sidebar',

    'before_widget' => '<aside class="widget %2$s">',

    'after_widget' => '</aside>',

    'before_title' => '<h2 class="widget-title">',

    'after_title' => '</h2>',

) );

}

add_action( 'widgets_init', 'header_widgets_init' );

...and in my header.php file I used:

<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('header_sidebar') ) : endif; ?>
  • Or instead of <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('header_sidebar') ) : endif; ?> should I use <?php dynamic_sidebar('header-sidebar'); ?>? I tried both and both work. – jrcollins Oct 3 '15 at 22:42
1

Short answer is yes, adding the widget directly in the child theme functions.php file is perfectly acceptable.

Some parent themes provide recommendations on how best to add new functionality within child themes (helpers, classes, specific design patterns, etc.) so the ideal approach may vary from one theme to the next.

I'm happy to elaborate if you can provide any additional information about your setup.

  • Hi, thanks for your help. The widget is for a banner ad in the header. It should be pretty simple. I was just wondering do I need to include the all of the code as posted above and could you tell me what ci_widgets_init and ci_theme are for? – jrcollins Oct 3 '15 at 9:52
  • The "ci_" bit is the namespace prefix for your theme. An abbreviation if you will. From there ci_widgets_init is the name of the function (which you can rename as desired) and ci_theme is a parameter that tells WordPress where to locate the translation for 'Blog Sidebar'. – dswebsme Oct 3 '15 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.