wordpress 如何在wordpress的header.php中添加widget?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15497201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:12:00  来源:igfitidea点击:

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

wordpresswordpress-plugin

提问by down1337

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

我已经安装了一个名为 的插件Translator Box,我使用它的短代码并将其放入我的 wordpress 主题 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.

它还在小部件部分的启用小部件处生成一个小部件。有没有办法在 header.php 中使用一些可以调用小部件的代码?谢谢你。

回答by Sven

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

您可以在 header.php 中定义一个部分来显示小部件。在你的functions.php中做这样的事情:

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:

现在包含在您的 header.php 文件中:

<?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.

在您的管理界面中,您现在应该有 2 个区域(“主侧边栏”和“标题区域”),您可以用小部件填充。

回答by Alec Sanger

use do_shortcode

使用do_shortcode

<?php echo do_shortcode( $content ) ?>