簡體   English   中英

在WordPress主題開發中使用相對路徑的正確方法是什么?

[英]what is the correct way of using relative paths in wordpress theme development?

我正在編寫一些用於wordpress主題開發的代碼,我打算將其重用於將來的主題(甚至可以上傳到github)。 它由幾十個文件以及一些javascript和css文件組成。

我將來唯一要做的承諾是,我所有的文件都將放置在同一目錄中,該目錄放置在主題目錄中的位置是未知的。

如果我不知道文件的絕對路徑(get_template_directory_uri。),該如何排隊文件(wp_enqueue_style,wp_enqueue_script函數)?

我也希望不要編寫包含include \\ require的十幾行代碼,而是編寫一個包含文件,該文件將通過它們的相對路徑包含其余文件。

如果您需要主題索引文件中的路徑,一個簡單的解決方案(不一定是最正確的方法)可能是這樣的:

 <?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"/>

希望有人覺得這有用。

有兩個主要的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() ,它可能會改變您對解決方案的想法。

您可以從WP函數下面檢索主題目錄URI,然后可以將文件名與相應的文件夾名一起傳遞。

get_template_directory_uri()

我最終使用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中編輯代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM