我想你误解了the_widget函数的使用。来自the WordPress Codex article的描述是:
此模板标签在侧边栏之外显示任意小部件。
不应在使用register sidebar 创建的自定义侧边栏中使用它。
如果你只有一个侧边栏布局,没有注册的 WordPress 侧边栏,那么你应该使用the_widget。
第二个参数,$instance,是小部件设置的数组或查询字符串。
例如,要在您将使用的模板中显示 WordPress 类别小部件:
$instance = array(
'title' => __( 'Categories' ),
'count' => 0,
'hierarchical' => 0,
'dropdown' => 0,
);
the_widget( 'WP_Widget_Categories', $instance );
如果您为联系表单使用自定义侧边栏,您可以使用 get_sidebar 在模板中显示它。
<?php get_sidebar('contact'); ?>
这将从文件wp-content/yourTheme/sidebar-contact.php 中提取。该侧边栏可以直接使用 the_widget 来显示您的文本小部件,或使用 dynamic_sidebar 来显示添加到 WordPress 仪表板侧边栏中的小部件。