down1337
down1337

Reputation: 143

How to add widget to the header.php in wordpress?

i have installled a pludgin whose named is Translator Box , i using its short code and put it into my wordpress theme header.php.

 [translation_box languages="english,russian,german,spanish,french,chinese" width="100%" height="200px" bgcolor="white" txtcolor="#000000"]

but is doesn't work!

it also generate a widget at Enabled widget in the widgets part. is there a way when using some code in header.php that can invoke the widget? thank you.

Upvotes: 8

Views: 33119

Answers (2)

Sven
Sven

Reputation: 171

You can define a part in your header.php to show widgets. In your functions.php make something like this:

function my_widgets_init() {

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

register_sidebar( array(
    'name' => __( 'Header Area', 'your-theme' ),
    'id' => 'sidebar-2',
    'description' => __( 'An optional widget area for your site header', 'your-theme' ),
    'before_widget' => '<div id="%1$s" class="headwidget %2$s">',
    'after_widget' => "</div>",
    'before_title' => '<h3>',
    'after_title' => '</h3>',
) );
}
add_action( 'widgets_init', 'my_widgets_init' );

The first part for example will be your widget area in the sidebar and the second your widget area in your header.

Now include in your header.php file:

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

where your widget should be.

In your admin interface you should now have 2 areas ('Main Sidebar' and 'Header Area') you can fill with widgets.

Upvotes: 16

Alec Sanger
Alec Sanger

Reputation: 4562

use do_shortcode

<?php echo do_shortcode( $content ) ?>

Upvotes: 2

Related Questions