在wordpress仪表板页面部分创建小部件区域?

时间:2014-02-28 07:08:32

标签: php wordpress admin dashboard

如何在wordpress页面部分创建窗口小部件区域。如果我在 functions.php 文件中添加以下代码,则在仪表板菜单部分创建的小部件我想在我的页面部分中添加额外的小部件,而不是在仪表板中。

将小部件添加到仪表板。

 //This function is hooked into the 'wp_dashboard_setup' action below.



function example_add_dashboard_widgets() {

wp_add_dashboard_widget(
             'example_dashboard_widget',         // Widget slug.
             'Example Dashboard Widget',         // Title.
             'example_dashboard_widget_function' // Display function.
    );  

}

add_action( 'wp_dashboard_setup', 'example_add_dashboard_widgets' );

创建函数以输出Dashboard Widget的内容。

function example_dashboard_widget_function() {

// Display whatever it is you want to show.

echo "Hello World, I'm a great Dashboard Widget";
} 

3 个答案:

答案 0 :(得分:0)

你应该在functions.php中注册侧边栏

if (function_exists('register_sidebar')) {  
     register_sidebar(array(  
      'name' => 'widget_name',  
      'id'   => 'widget-id',  
      'description'   => 'Type something here',  
      'before_widget' => '<div id="one" class="two">',  
      'after_widget'  => '</div>',  
      'before_title'  => '<h2>',  
      'after_title'   => '</h2>'  
     ));  
    }

它会在仪表板中注册侧边栏(小部件)。

现在你可以随时随地
<?php dynamic_sidebar('widget-id'); ?>

答案 1 :(得分:0)

此功能已存在于默认主题

    function twentyfourteen_widgets_init() {
    require get_template_directory() . '/inc/widgets.php';
    register_widget( 'Twenty_Fourteen_Ephemera_Widget' );

    register_sidebar( array(
        'name'          => __( 'Primary Sidebar', 'twentyfourteen' ),
        'id'            => 'sidebar-1',
        'description'   => __( 'Main sidebar that appears on the left.', 'twentyfourteen' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget'  => '</aside>',
        'before_title'  => '<h1 class="widget-title">',
        'after_title'   => '</h1>',
    ) );
    register_sidebar( array(
        'name'          => __( 'Content Sidebar', 'twentyfourteen' ),
        'id'            => 'sidebar-2',
        'description'   => __( 'Additional sidebar that appears on the right.', 'twentyfourteen' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget'  => '</aside>',
        'before_title'  => '<h1 class="widget-title">',
        'after_title'   => '</h1>',
    ) );
    register_sidebar( array(
        'name'          => __( 'Footer Widget Area', 'twentyfourteen' ),
        'id'            => 'sidebar-3',
        'description'   => __( 'Appears in the footer section of the site.', 'twentyfourteen' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget'  => '</aside>',
        'before_title'  => '<h1 class="widget-title">',
        'after_title'   => '</h1>',
    ) );
}
add_action( 'widgets_init', 'twentyfourteen_widgets_init' );

你可以通过复制和粘贴这个来创建多个小部件区域,并且每次都使用#来创建id

register_sidebar( array(
        'name'          => __( 'new area', 'twentyfourteen' ),
        'id'            => 'sidebar-5',
        'description'   => __( 'Appears in the footer section of the site.', 'twentyfourteen' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget'  => '</aside>',
        'before_title'  => '<h1 class="widget-title">',
        'after_title'   => '</h1>',
    ) );
}

答案 2 :(得分:0)

在function.php中添加:

function create_widget($name, $id, $description) {

    register_sidebar(array(
        'name' => __( $name ),    
        'id' => $id,
        'description' => __( $description ),
        'before_widget' => '<div id="'.$id.'" class="widget %1$s %2$s">',
        'after_widget' => '</div>',
        'before_title' => '<h3>',
        'after_title' => '</h3>'
    ));

}


    create_widget("Header", "uptop", "Displays in the header of the site, above the title"); // Create the actual widgets

然后添加下一个代码你想使用这个区域,例如你的header.php:

<?php if ( !dynamic_sidebar('uptop') ); ?>