WordPress Widget Text - 如何手动在任何地方显示它?

WordPress Widget Text - how to display it anywhere manually?

如何手动获取 WP 小部件文本?

我尝试显示以下代码:

<?php the_widget( 'WP_Widget_Text' ); ?>

但是我没有任何显示。

我需要在此函数中传递什么具体信息吗?我创建了小部件文本并将其命名为 'Follow Me':

有什么想法吗?

如果你想调用小部件使用这个函数

<?php the_widget( 'your-widget-title' ); ?> 

为您的小部件

<?php the_widget( 'Follow Me' ); ?>

创建侧边栏并将文本小部件放入该侧边栏中。

假设你创建的侧边栏id的ID"sidebar-2"

如果只想显示Text Widget,则不要在sidebar-2中添加其他widget,即为Text Widget创建一个单独的sidebar,因为sidebar中包含的所有widget都是由以下函数调用的。

您可以将此函数粘贴到您希望显示文本小部件的位置。

if ( is_active_sidebar( 'sidebar-2' ) ) : ?>
    <div id="tertiary" class="sidebar-container" role="complementary">
        <div class="sidebar-inner">
            <div class="widget-area">
                <?php dynamic_sidebar( 'sidebar-2' ); ?>
            </div><!-- .widget-area -->
        </div><!-- .sidebar-inner -->
    </div><!-- #tertiary -->
<?php endif; ?>

仅为文本小部件创建新的 siebar:

add_action( 'widgets_init', 'theme_slug_widgets_init' );
function theme_slug_widgets_init() {
    register_sidebar( array(
        'name' => __( 'Main Sidebar', 'theme-slug' ),
        'id' => 'sidebar-2',
        'description' => __( 'Widgets in this area will be shown on all posts and pages.', 'theme-slug' ),
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
    'after_widget'  => '</li>',
    'before_title'  => '<h2 class="widgettitle">',
    'after_title'   => '</h2>',
    ) );
}

ID of the sidebar is used to call the sidebar at required place in our case "sidebar-2"

ID of the sidebar should be in lower case and can contain "-".