Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 522
down1337 Avatar asked Mar 19 '13 10:03

down1337


People also ask

How do I add a widget code in WordPress?

Go to Appearance > Customize in the WordPress Administration Screens. Click the Widget menu in the Theme Customizer to access to the Widget Customize Screen. Click the down arrow of Widget Area to list the already registered Widgets. Click Add a Widget button at the bottom of sidebar.

How do I create a custom header in WordPress?

Go to WordPress Dashboard > Templates > ThemeBuilder. Click Add New Template and choose Header (or Footer) Name your header template and click Create Header (or Footer) Now you'll be able to either choose a premade header (or footer) template or create one from scratch.

How do I add a widget to a template?

Drop a widget on page or template You add widgets the editing mode of pages and templates. To edit the content of a page, on Pages page, perform one of the following: Click the page or the template that you want to edit. Click the Actions link of a page or template, and then choose Content.


1 Answers

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.

like image 194
Sven Avatar answered Sep 20 '22 15:09

Sven