我在 WordPress 中使用此代码添加了我的自定义动态侧边栏
register_sidebar
(array(
'name' => __( 'My widget' ),
'id' => 'right-sidebar',
'description' => __( 'Widgets in this area will be shown on the right-hand side.' ),
'before_title' => '<h1>',
'after_title' => '</h1>'
));
现在我可以通过 Dashboard -> Appearance -> Widgets 在 WordPress 管理中看到这个动态侧边栏。
当我激活主题时,默认情况下这个动态侧边栏不显示任何内容;要显示小部件(例如最近的帖子、页面、类别),我必须将它们拖到我的小部件动态侧边栏中。
但是我希望My Widget 动态侧边栏默认显示三个Widgets(最近的帖子、页面、类别),类似于20 或211 中的动态侧边栏。
最佳答案
这很容易做到,使用 dynamic_sidebar()
和 the_widget()
.
这是用于显示默认内容的构造,如果没有向动态侧边栏添加小部件:
<?php
if ( ! dynamic_sidebar( 'My_Widget' ) ) {
// default content goes here
}
?>
因此,要将特定小部件输出为默认内容,只需调用
the_widget($widget, $instance, $args)
.例如,要显示 "Recent Posts" Widget :<?php
if ( ! dynamic_sidebar( 'My_Widget' ) ) {
the_widget( 'WP_Widget_Recent_Posts' );
}
?>
(有关其他用法示例,请参阅链接的 Codex 引用资料,以及每个小部件的
$instance
/$args
值。)
关于wordpress - 如何将默认小部件添加到自定义动态侧边栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10203413/