-- 创造无限可能

WordPress加载模版相关函数

2022-05-17 00:16:53
356 人浏览 5 人点赞
有用,点赞支持一下

加载模版

一个网站,通常由多种类型的页面组成。但不同的页面,会有一个功能相同的部分,为了方便管理相同的部分,我们可以把相同的部分提取出来,做成一个独立的模版,以供不同的页面引用,方便维护。

常用的与模版相关的函数

  • get_header() 加载公共头部
  • get_sidebar() 加载公共侧边栏模版
  • get_footer() 加载公共底部模版
  • get_template_part() 加载自定义的模版
  • 常用函数的具体说明和用法

  • get_header()

    /**
    * 加载公共头部
    * 常使用于一个模版文件的头部,用于引入公共的头部模版
    * 如果有多个头部模版,可以通过参数$name区分,对应的模版文件名:header-$name.php
    *
    * @param string $name 根据$name引入不同的头部.
    * @param array  $args 传入模版的参数
    */
    function get_header( $name = null, $args = array() ) {
      do_action( 'get_header', $name, $args );
    
      $templates = array();
      $name      = (string) $name;
      if ( '' !== $name ) {
          $templates[] = "header-{$name}.php";
      }
    
      $templates[] = 'header.php';
          // 请求文件
      if ( ! locate_template( $templates, true, true, $args ) ) {
          return false;
      }
    }
    
  • get_sidebar()

    /**
    * 加载公共侧边栏模版
    *
    * 通常情况下,侧边栏目都是通用的,默认加载根目录下的sidebar.php文件
    * @param string $name 如果有多个侧边栏,可以通过$name区分,对应模版名为:sidebar-$name.php
    * @param array  $args 传入模版的参数
    * @return void|false 成功则返回调用的模版
    */
    function get_sidebar( $name = null, $args = array() ) {
      /**
       * Fires before the sidebar template file is loaded.
       *
       * @since 2.2.0
       * @since 2.8.0 The `$name` parameter was added.
       * @since 5.5.0 The `$args` parameter was added.
       *
       * @param string|null $name Name of the specific sidebar file to use. Null for the default sidebar.
       * @param array       $args Additional arguments passed to the sidebar template.
       */
      do_action( 'get_sidebar', $name, $args );
    
      $templates = array();
      $name      = (string) $name;
      if ( '' !== $name ) {
          $templates[] = "sidebar-{$name}.php";
      }
    
      $templates[] = 'sidebar.php';
    
      if ( ! locate_template( $templates, true, true, $args ) ) {
          return false;
      }
    }
    
  • get_footer()

    /**
    * 加载公共底部模版
    * 通常情况下,网站每个页面的底部都是通用的,默认加载根目录下的footer.php文件
    *
    * @param string $name 如果有多个底部模版,可以通过$name区分,对应模版名为:footer-$name.php
    * @param array  $args 传入模版的参数
    * @return void|false 成功则返回调用的模版
    */
    function get_footer( $name = null, $args = array() ) {
      /**
       * Fires before the footer template file is loaded.
       *
       * @since 2.1.0
       * @since 2.8.0 The `$name` parameter was added.
       * @since 5.5.0 The `$args` parameter was added.
       *
       * @param string|null $name Name of the specific footer file to use. Null for the default footer.
       * @param array       $args Additional arguments passed to the footer template.
       */
      do_action( 'get_footer', $name, $args );
    
      $templates = array();
      $name      = (string) $name;
      if ( '' !== $name ) {
          $templates[] = "footer-{$name}.php";
      }
    
      $templates[] = 'footer.php';
    
      if ( ! locate_template( $templates, true, true, $args ) ) {
          return false;
      }
    }
    
  • get_template_part()

    /**
    * 加载自定义的模版
    *
    * 因为业务的需求,会自定义一些通用的模版,就可以通过get_template_part方法引入
    *
    * @param string $slug 这里指模版的通用路径.
    * @param string $name 针对同一个类型有不同的模版可以通过name区分.默认为空
    * @param array  $args 传入模版的参数
    * @return void|false Void on success, false if the template does not exist.
    */
    function get_template_part( $slug, $name = null, $args = array() ) {
      /**
       * Fires before the specified template part file is loaded.
       *
       * The dynamic portion of the hook name, `$slug`, refers to the slug name
       * for the generic template part.
       *
       * @since 3.0.0
       * @since 5.5.0 The `$args` parameter was added.
       *
       * @param string      $slug The slug name for the generic template.
       * @param string|null $name The name of the specialized template.
       * @param array       $args Additional arguments passed to the template.
       */
      do_action( "get_template_part_{$slug}", $slug, $name, $args );
    
      $templates = array();
      $name      = (string) $name;
      if ( '' !== $name ) {
          $templates[] = "{$slug}-{$name}.php";
      }
    
      $templates[] = "{$slug}.php";
    
      /**
       * Fires before a template part is loaded.
       *
       * @since 5.2.0
       * @since 5.5.0 The `$args` parameter was added.
       *
       * @param string   $slug      The slug name for the generic template.
       * @param string   $name      The name of the specialized template.
       * @param string[] $templates Array of template files to search for, in order.
       * @param array    $args      Additional arguments passed to the template.
       */
      do_action( 'get_template_part', $slug, $name, $templates, $args );
    
      if ( ! locate_template( $templates, true, false, $args ) ) {
          return false;
      }
    }