【问题标题】:what is the correct way of using relative paths in wordpress theme development?在 wordpress 主题开发中使用相对路径的正确方法是什么?
【发布时间】:2017-04-19 17:55:43
【问题描述】:

我正在编写一些用于 wordpress 主题开发的代码,我计划将其重用于未来的主题(甚至可能上传到 github)。 它由几十个文件和一些 javascript 和 css 文件组成。

我愿意为未来做出的唯一承诺是我所有的文件都将放在同一个目录中,这个目录将放在主题目录中的哪个位置是未知的。

如果我不知道文件的绝对路径(get_template_directory_uri .''),我应该如何将文件(wp_enqueue_style、wp_enqueue_script 函数)排入队列?

我还希望我可以编写一个包含文件,而不是包含十几行 include\require,而该文件将通过它们的相对路径包含其余文件。

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案1】:

    如果您需要主题索引文件中的路径,一个简单的解决方案(不一定是最合适的方法)可能是这样的:

     <?php
            $TEMPLATE_PATH = get_template_directory_uri(); 
            $TEMPLATE_PATH = parse_url($TEMPLATE_PATH, PHP_URL_PATH);
          ?>
    

    然后您就可以使用 $TEMPLATE_PATH 作为相对路径,如下所示:

    <link href="<?php echo $TEMPLATE_PATH; ?>/favicon.ico" rel="shortcut icon" type="image/x-icon"/>
    

    输出如下:

    <link href="/wp-content/themes/ThemesName/favicon.ico" rel="shortcut icon" type="image/x-icon"/>
    

    希望有人觉得这很有用。

    【讨论】:

      【解决方案2】:

      有两个主要的 WP 函数可用于查找主题中的相对路径:

      get_template_directory()

      get_template_directory_uri()

      您可以创建一个名为“可重用”的主题子目录或类似的名称,这是一个顶级子目录。此外,假设您的文件集如下:

      mytheme/reusable/loader.php
      mytheme/reusable/include-me.php
      mytheme/reusable/include-another.php
      mytheme/reusable/js/reuse.js
      mytheme/reusable/css/reuse.css
      

      loader.php:

      <?php
      // add as many files as you want to this array
      $include_paths = array(
          'reusable/include-me.php',
          'reusable/include-another.php',
      );
      // loop through the paths and include them if they exist
      $template_directory = trailingslashit(get_template_directory());
      foreach( $include_paths as $path ) {
          $complete_path = $template_directory . $path;
          if( file_exists( $complete_path ) ) {
              include($complete_path);
          }
      }
      // function to enqueue js and css
      function reusable_enqueue_scripts() {
          $template_directory_uri = get_template_directory_uri();
          wp_enqueue_style( 'reusable-style', $template_directory_uri . '/css/reuse.css');
          wp_enqueue_script( 'reusable-js', $template_directory_uri . '/js/reuse.js');
      }
      // add function to enqueue action hook
      add_action( 'wp_enqueue_scripts', 'reusable_enqueue_scripts' );
      

      然后在你主题的functions.php文件中:

      $reusable = trailingslashit(get_template_directory()).'reusable/loader.php';
      include($reusable);
      

      还有一个非常有用的 WP 函数,称为 get_template_part(),您可以使用 look into,可能会改变您对解决方案的思考方式。

      【讨论】:

      • 您的答案假定我的文件始终位于“mytheme/reusable”目录中,但也可以是“mytheme/inc”或其他目录。我希望它是动态的。我成功地测试了我的所有文件在一个目录中以及一个类似于这样的 loader.php 文件:include ('file1.php'); //relative to loader.php include ('file2.php'); 然后在 function.php 中:include (get_template_directory() . 'dynamic-path/loader.php'); 所以我不依赖常量路径。正如我所说,它适用于 php 文件,但我解决了它的入队部分。我也不知道它是否会在将来起作用或引起问题
      【解决方案3】:

      您可以从 WP 函数下方检索主题目录 URI,然后您可以使用相应的文件夹名称传递文件名。

      get_template_directory_uri()
      

      【讨论】:

        【解决方案4】:

        我最终使用dirname(__FILE__) 来确定 loader.php 的位置, 并从中减去get_template_directory() 以获得我的代码在主题目录中的相对路径,如下所示: $MY_PATH = str_replace(realpath(get_template_directory()),"",dirname(__FILE__)));

        最终结果 load.php:

        $MY_PATH =  str_replace(realpath(get_template_directory()),"",dirname(__FILE__)));
        
        require_once('file1.php');
        require_once('file2.php');
        require_once('file3.php');
        
        function my_scripts() {
            $mypath = $GLOBALS['MY_PATH'];
            wp_enqueue_style( 'my-style', $template_directory_uri . $mypath . '/style.css');
            wp_enqueue_script( 'my-js', $template_directory_uri . $mypath . '/script.js');
        }
        
        add_action( 'wp_enqueue_scripts', 'my_scripts' );
        

        现在我可以更改我的代码目录位置,而无需编辑 loader.php 中的代码。

        【讨论】:

          猜你喜欢
          • 2012-06-13
          • 1970-01-01
          • 1970-01-01
          • 2014-03-29
          • 2017-06-18
          • 1970-01-01
          • 1970-01-01
          • 2015-12-23
          • 1970-01-01
          相关资源
          最近更新 更多