首页 视频课程 WordPress开发文档菜单/小工具/短代码 注册区域

注册区域

2020-04-08 / 3456阅

本视频需要登录以后才能播放

登录

WordPress自带一部分小工具,但是你开发的主题前台默认是没有小工具区域的,可以使用以下代码注册小工具区域。
小工具区域是用来展示小小工具的地方,代码如下:

function youran_register_widget_areas() {
      register_sidebar( array(
          'name' => 'Footer area one',
          //小工具的区域名称,默认是 'Sidebar' 加 数字 ID
          'id'=> 'footer_area_one',
          //区域的ID,默认是一个自动递增的数字 ID
          'description'=> 'This widget area discription',
          //区域的描述,默认为空
          'before_widget' => '',
          //区域的内容前的HTML代码,默认: '')
          'after_widget'=> '',
          //区域内容后的HTML代码,默认: "\n"
          'before_title'=> '',
          //区域标题前的HTML代码,默认: 
          'after_title'=> '',
          //区域标题后的HTML代码,默认:"\n"
      ));
 }
 add_action( 'widgets_init', 'youran_register_widget_areas' ); 

注册好区域后,你可以通过后台-外观-小工具设置中给这个区域添加小工具。前台需要把这个小工具位置放到合适的位置,才会显示。只注册不调用,是不会显示小工具的。

function youran_register_widget_areas_more(){
       $number=10;
       $args = array(
              'name' => __('Sidebar %d'),
              'id' => 'sidebar',
              'description' => '',
              'class' => '',
              'before_widget' => '<li id="%1$s" class="widget %2$s">',
              'after_widget' => '</li>',
              'before_title' => '<h2 class="widgettitle">',
              'after_title' => '</h2>'
       );
       register_sidebars($number, $args);
}
add_action( 'widgets_init', 'youran_register_widget_areas_more' );  

阅读文章或者观看视频过程中有任何问题,请下方留言或者联系我Q248758228

大家谈论
    我的见解
    目录