2014-02-28 75 views
1

如何在wordpress页面部分创建一个小部件区域。如果我在我的functions.php文件中添加以下代码,那么在仪表板菜单部分中创建的小部件我想在我的页面部分中添加其他小部件,而不是在仪表板中。在wordpress仪表板页面部分创建小部件区域?

将小部件添加到仪表板。

//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"; 
} 

回答

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'); ?> 
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>', 
    )); 
} 
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')); ?> 
相关问题