【问题标题】:Create widget area in wordpress dashboard pages section?在 wordpress 仪表板页面部分创建小部件区域?
【发布时间】:2014-04-01 01:32:57
【问题描述】:

如何在 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";
} 

【问题讨论】:

    标签: php wordpress admin dashboard


    【解决方案1】:

    你应该在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'); ?>
    

    【讨论】:

      【解决方案2】:

      默认主题中已存在此功能

          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>',
          ) );
      }
      

      【讨论】:

        【解决方案3】:

        在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') ); ?>
        

        【讨论】:

          猜你喜欢
          • 2012-01-29
          • 1970-01-01
          • 2014-04-02
          • 1970-01-01
          • 2023-03-17
          • 2018-04-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多